英文:
How do I convert int to String and still count?
问题
以下是您的代码翻译结果:
int m, n, sum = 0;
System.out.println("Enter the numbers you want to add");
m = sc.nextInt();
while (m > 0) {
    n = m % 10;
    sum = sum + n;
    m = m / 10;
}
System.out.println("Sum of the numbers: " + sum);
}
英文:
So this is my code right know. The code works with putting in 123 and I get 6 as answer. But if I put 1 2 3 I get 1 as answer. I want the code to convert into a string, but if I putt a space I will give a error message but don't know how to do it. I have try different things but getting errors every time, so can someone help me out? I still want the code to count the sum of 123 but give a error message, "try again, with no space" if I put 1 2 3 in.
int m, n, sum = 0;
System.out.println("Skriv in de tal som du vill addera");
m = sc.nextInt();
    
while (m > 0) {
    n = m % 10;
    sum = sum + n;
    m = m / 10;
}
System.out.println("Summan av talen:" + sum);
}
答案1
得分: 3
如果您想在输入包含空格时获得错误消息,请尝试以下代码片段:
String m;
int n, sum = 0;
System.out.println("Skriv in de tal som du vill addera:");
try {
    m = sc.nextLine();
    int mm = Integer.parseInt(m);
    while (mm > 0) {
        n = mm % 10;
        sum = sum + n;
        mm = mm / 10;
    }
    System.out.println("Summan av talne: " + sum);
} catch (Exception e) {
    System.out.println("试着再次输入,不要包含空格。");
}
这应该像您想的那样捕获异常,当字符串无法转换为整数时。
英文:
If you want to get an error message when an input has spaces, try this piece of code:
String m;
int n, sum = 0;
		System.out.println("Skriv in de tal som du vill addera: ");
		try {
		m = sc.nextLine();
		int mm=Integer.parseInt(m);
		while (mm > 0) {
		    n = mm % 10;
		    sum = sum + n;
		    mm = mm / 10;
		    
		}
		System.out.println("Summan av talne: "+sum);
		}catch (Exception e) {
			System.out.println("try again, with no spaces.");
		}
This should catch the exception like you want when a string cannot be converted to int.
答案2
得分: 2
你需要将输入作为字符串读取(使用Scanner的nextLine()方法),而不是作为整数,并将其存储在一个String类型的变量中。然后,你可以使用replace()将空格替换为空字符串,并将其转换为整数(parseInt();应该可以工作),显示消息,然后像你现在正在做的方式继续进行。
英文:
You'll need to read the input as a String (using the nextLine() method of Scanner), not as an integer, and store it in a variable of type String. Then, you can replace() the spaces with an empty string, and cast or convert it to int (parseInt(); should work), show the message and proceed like you are doing now.
答案3
得分: 1
使用 Scanner#nextLine 方法将输入作为 String 进行扫描。如果输入字符串包含空格,则分割输入字符串并处理其中的每个整数字符串;否则,使用 Ineteger#parseInt 将输入字符串解析为 int,然后按照您已经在进行的方式进行处理。
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        int m, n, sum = 0;
        Scanner sc = new Scanner(System.in);
        System.out.print("Skriv in de tal som du vill addera: ");
        String input = sc.nextLine();
        if (input.contains(" ")) {
            // Split the input on optional space
            String[] nums = input.split("\\s+");
            // Process each integer string from the input
            for (String num : nums) {
                sum += Integer.parseInt(num);
            }
        } else {
            m = Integer.parseInt(input);
            while (m > 0) {
                n = m % 10;
                sum = sum + n;
                m = m / 10;
            }
        }
        System.out.println("Summan av talen: " + sum);
    }
}
一个示例运行:
Skriv in de tal som du vill addera: 123
Summan av talen: 6
另一个示例运行:
Skriv in de tal som du vill addera: 1 2 3
Summan av talen: 6
英文:
Use Scanner#nextLine to scan the input as a String. If the input string contains space, split the input string and process each integer string in it; otherwise, parse the input string into an int using Ineteger#parseInt and then process it in the way you are already doing.
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		int m, n, sum = 0;
		Scanner sc = new Scanner(System.in);
		System.out.print("Skriv in de tal som du vill addera: ");
		String input = sc.nextLine();
		if (input.contains(" ")) {
			// Split the input on optional space
			String[] nums = input.split("\\s+");
			// Process each integer string from the input
			for (String num : nums) {
				sum += Integer.parseInt(num);
			}
		} else {
			m = Integer.parseInt(input);
			while (m > 0) {
				n = m % 10;
				sum = sum + n;
				m = m / 10;
			}
		}
		System.out.println("Summan av talen: " + sum);
	}
}
A sample run:
Skriv in de tal som du vill addera: 123
Summan av talen: 6
Another sample run:
Skriv in de tal som du vill addera: 1 2 3
Summan av talen: 6
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论