我的输入字符串在运行代码时不会出现。首先它表示当我在我的Main方法中使用它时它无法使用。从我的Main方法中取出它并将它放在Public类中后,无法解决的问题就消失了。但是在运行代码时它仍然不会出现。
B
答案 0 :(得分:0)
应该是这样的吗?
public static void main(String[] args) {
boolean digitEntered = false;
String input = "You enetred number : ";
while (digitEntered == false) {
System.out.print("Type a number between 0 and 9 : ");
Scanner scan = new Scanner(System.in);
int x = scan.nextInt();
if (x >= 0 && x <= 9) {
digitEntered = true;
input += x;
} else {
System.out.println("Your number was bigger than 9 ");
}
}
System.out.println(input);
}
答案 1 :(得分:0)
用于终止标记循环的Java break语句。并且您的字符串input
包含值。尝试在while循环后访问它。
public class Number0to9 {
public static String input;
public static void main(String[] args) {
tryagain : while (true) {
System.out.print("Type a number between 0 and 9 : ");
Scanner scan = new Scanner(System.in);
int x = scan.nextInt();
int output = x;
input = "You enetred number : ";
switch (x) {
case 0:
input += output;
break tryagain;
case 1:
input += output;
break tryagain;
case 2:
input += output;
break tryagain;
case 3:
input += output;
break tryagain;
case 4:
input += output;
break tryagain;
case 5:
input += output;
break tryagain;
case 6:
input += output;
break tryagain;
case 7:
input += output;
break tryagain;
case 8:
input += output;
break tryagain;
case 9:
input += output;
break tryagain;
default:
System.out.println("Your number was bigger than 9 ");
break;
}
}
System.out.println(input);
}
}
答案 2 :(得分:0)
input += output;
只有当两个变量都是int(或任何其他算术变量类型)时,上面的代码语句才能用于添加输入和输出。
如果要连接输入和输出,首先应将输出转换为String。
input = input + Integer.toString(output);
在每种情况下进行此更改。