进入';y';代码应该再次请求选择,但没有发生 import java.util.Scanner; 公共类开关箱{ 公共静态void main(字符串[]args){ 扫描仪sc=新的扫描仪(System.in); 字符串ch=“NULL”; 做 { System.out.println(“输入您的选择:”); 字符串str=sc.next(); 开关(str) { 案例“添加”: { System.out.println(“输入2个:”); int a=sc.nextInt(); int b=sc.nextInt(); INTC=a+b; System.out.println(“adition为:+c”); 打破 } 案例“Sub”: { System.out.println(“输入2个:”); int a1=sc.nextInt(); int a2=sc.nextInt(); int a3=a1-a2; 系统输出打印项次(“子项为:+a3); 打破 } “乘法”情况: { System.out.println(“输入2个:”); int a1=sc.nextInt(); int a2=sc.nextInt(); int a3=a1*a2; 系统输出打印项次(“子项为:+a3); 打破 } “划分”一案: { System.out.println(“输入2个:”); int a1=sc.nextInt(); int a2=sc.nextInt(); int a3=a1/a2; 系统输出打印项次(“子项为:+a3); 打破 } 案例“Fib”: { System.out.println(“输入范围:”); int n=sc.nextInt(); 如果(n==0) { 系统输出打印项次(“0”); } else如果(n==1) { 系统输出打印项次(“01”); } 其他的 { 系统输出打印(“01”); int a=0; int b=1; 对于(int i=1;i
在while循环之后,当用户输入“y”时,它应该询问“您想继续吗”,但同样的情况不会发生。我调试了代码,它运行正常,直到“您想继续吗?”之后: 在eclipse中调试时出现SourceNotFound错误进入';y';代码应该再次请求选择,但没有发生 import java.util.Scanner; 公共类开关箱{ 公共静态void main(字符串[]args){ 扫描仪sc=新的扫描仪(System.in); 字符串ch=“NULL”; 做 { System.out.println(“输入您的选择:”); 字符串str=sc.next(); 开关(str) { 案例“添加”: { System.out.println(“输入2个:”); int a=sc.nextInt(); int b=sc.nextInt(); INTC=a+b; System.out.println(“adition为:+c”); 打破 } 案例“Sub”: { System.out.println(“输入2个:”); int a1=sc.nextInt(); int a2=sc.nextInt(); int a3=a1-a2; 系统输出打印项次(“子项为:+a3); 打破 } “乘法”情况: { System.out.println(“输入2个:”); int a1=sc.nextInt(); int a2=sc.nextInt(); int a3=a1*a2; 系统输出打印项次(“子项为:+a3); 打破 } “划分”一案: { System.out.println(“输入2个:”); int a1=sc.nextInt(); int a2=sc.nextInt(); int a3=a1/a2; 系统输出打印项次(“子项为:+a3); 打破 } 案例“Fib”: { System.out.println(“输入范围:”); int n=sc.nextInt(); 如果(n==0) { 系统输出打印项次(“0”); } else如果(n==1) { 系统输出打印项次(“01”); } 其他的 { 系统输出打印(“01”); int a=0; int b=1; 对于(int i=1;i,java,Java,在while循环之后,当用户输入“y”时,它应该询问“您想继续吗”,但同样的情况不会发生。我调试了代码,它运行正常,直到“您想继续吗?”之后: 在eclipse中调试时出现SourceNotFound错误 import java.util.Scanner; public class SwitchCase { public static void main(String[] args) { Scanner sc=new Scanner(System.in);
import java.util.Scanner;
public class SwitchCase {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String ch="NULL";
do
{
System.out.println("Enter your choice:");
String str=sc.next();
switch(str)
{
case "Add":
{
System.out.println("Enter 2 nos:");
int a=sc.nextInt();
int b=sc.nextInt();
int c=a+b;
System.out.println("Adiition is:"+c);
break;
}
case "Sub":
{
System.out.println("Enter 2 nos:");
int a1=sc.nextInt();
int a2=sc.nextInt();
int a3=a1-a2;
System.out.println("Sub is:"+a3);
break;
}
case "Multiply":
{
System.out.println("Enter 2 nos:");
int a1=sc.nextInt();
int a2=sc.nextInt();
int a3=a1*a2;
System.out.println("Sub is:"+a3);
break;
}
case "Divide":
{
System.out.println("Enter 2 nos:");
int a1=sc.nextInt();
int a2=sc.nextInt();
int a3=a1/a2;
System.out.println("Sub is:"+a3);
break;
}
case "Fib":
{
System.out.println("Enter the range:");
int n=sc.nextInt();
if (n == 0)
{
System.out.println("0");
}
else if (n == 1)
{
System.out.println("0 1");
}
else
{
System.out.print("0 1 ");
int a = 0;
int b = 1;
for (int i = 1; i < n; i++)
{
int nextNumber = a + b;
System.out.print(nextNumber + " ");
a = b;
b = nextNumber;
}
}
}
}
System.out.println("Do you want to continue? y or n");
ch=sc.next();
} while(ch=="y");
}
}
使用等于而不是运算符“==”
查看Oracle教程:
使用等于而不是运算符“==”
检查Oracle教程:在您比较的while条件下
ch==“y”
。在比较字符串时,应使用如下equals
方法“y”.equals(ch)
。相等运算符将测试这两个值是否具有相同的参考值,显然,它们不具有相同的参考值。在您正在比较的while条件下ch==“y”
。在比较字符串时,应使用如下equals
方法“y”.equals(ch)
。相等运算符将测试这两个值是否具有相同的参考值,这显然是不相同的。@piet.t Nice catch,但这段代码甚至不应该根据我所看到的编译<代码>未找到源。。。这听起来像是IDE中的代码与您正在运行的代码不同步。这里可能隐藏着另一个bugsc.nextInt()<代码>下一步()
不读取分隔符。你的意思是nextLine()
@Tom是的。我想到的sc.nextLine()
与sc.nextLine(),但这段代码不应该根据我所看到的编译<代码>未找到源
。。。这听起来像是IDE中的代码与您正在运行的代码不同步。这里可能隐藏着另一个bugsc.nextInt()<代码>下一步()
不读取分隔符。你的意思是nextLine()
@Tom是的。我想到的sc.nextLine()
与sc.nextLine()。这类问题已经被问了数千次,投票结束,因为重复是正确的处理方法。不要发布对重复的讣告的答案。这类问题已经被问了数千次,投票结束,因为重复是正确的处理方法。不要发布对重复的讣告的答案。这类问题已经被问了数千次,投票结束,因为重复是正确的处理方法。不要发布对重复的讣告的答案。这类问题已经被问了数千次,投票结束,因为重复是正确的处理方法。
while(ch.equals("y"));