我正在编写一个代码,用户试图找到Triangle的斜边。我的代码首先询问用户他们想从A侧还是B侧开始。我使用Switch方法来覆盖这两种情况。在使用任何一种情况之后我想要我的代码做的是回到起点,直到用户退出。出于某种原因,我的代码只做了一个案例,但没有回到开头开始一个全新的循环。我使用了错误的方法吗?我是不是写了不正确的代码?我该怎么做才能解决它?
import java.util.Scanner;
public class HandleException {
public static void main (String[] args){
int data= 0;
int data1= 0;
int sum= 0;
Scanner input = new Scanner (System.in);
//ask user for a command
System.out.println("Which side would you like to start with A or B");
String s= input.next();
char letter= s.charAt(0);
loop:while (letter!= 'q'){
switch (letter){
//if user inputs 'A'
case 'a':
System.out.println("Enter value for side A: ");
data= input.nextInt();
System.out.println("Enter next command: A : value for side A; B : Value for side B; C: Calculate; Q : Quit program.");
s=input.next();
letter=s.charAt(0);
if (letter == 'b')
System.out.println("Enter Value For side B: ");
data1= input.nextInt();
System.out.println("Enter next command: A : value for side A; B : Value for side B; C: Calculate; Q : Quit program.");
s=input.next();
letter=s.charAt(0);
if (letter== 'c')
sum=data*data+data1*data1;
int sumsrt = (int) Math.sqrt(sum);
System.out.println("The hypotenuse is: "+sumsrt);
continue;
//if user inputs 'B'
case 'b':
System.out.println("Enter value for side B: ");
data= input.nextInt();
System.out.println("Enter next command: A : value for side A; B : Value for side B; C: Calculate; Q : Quit program.");
s=input.next();
letter=s.charAt(0);
if (letter == 'a')
System.out.println("Enter Value For side a: ");
data1= input.nextInt();
System.out.println("Enter next command: A : value for side A; B : Value for side B; C: Calculate; Q : Quit program.");
s=input.next();
letter=s.charAt(0);
if (letter== 'c')
sum=data*data+data1*data1;
int sumsrt1 = (int) Math.sqrt(sum);
System.out.println("The hypotenuse is: "+sumsrt1);
continue;
}
}
}
}
答案 0 :(得分:0)
第一次循环结束后,它会转到第while(letter!= 'q')
行
和字母值为c
开关中没有匹配值,因此第二次循环将结束并且什么都不做。
它会保持循环,所以你没有机会问下一个System.in
答案 1 :(得分:0)
一旦完成斜边计算,你不会要求退出char。这就是为什么程序进入无限循环而不是退出也是你在完成后使用break。
if (letter == 'b')
System.out.println("Enter Value For side B: ");
data1= input.nextInt();
System.out.println("Enter next command: A : value for side A; B : Value for side B; C: Calculate; Q : Quit program.");
s=input.next();
letter=s.charAt(0);
if (letter== 'c')
sum=data*data+data1*data1;
int sumsrt = (int) Math.sqrt(sum);
System.out.println("The hypotenuse is: "+sumsrt);
System.out.println("Press q for quit otherwise enter side ");
break;
同样适用于case 'b'
答案 2 :(得分:0)
切换案例后再次读取输入。试试这个
import java.util.Scanner;
public class HandleException {
public static void main(String[] args) {
int data = 0;
int data1 = 0;
int sum = 0;
Scanner input = new Scanner(System.in);
// ask user for a command
System.out.println("Which side would you like to start with A or B");
String s = input.next();
char letter = s.charAt(0);
while (letter != 'q') {
switch (letter) {
// if user inputs 'A'
case 'a':
System.out.println("Enter value for side A: ");
data = input.nextInt();
System.out.println("Enter next command: A : value for side A; B : Value for side B; C: Calculate; Q : Quit program.");
s = input.next();
letter = s.charAt(0);
if (letter == 'b')
System.out.println("Enter Value For side B: ");
data1 = input.nextInt();
System.out.println("Enter next command: A : value for side A; B : Value for side B; C: Calculate; Q : Quit program.");
s = input.next();
letter = s.charAt(0);
if (letter == 'c')
sum = data * data + data1 * data1;
int sumsrt = (int) Math.sqrt(sum);
System.out.println("The hypotenuse is: " + sumsrt);
break;
// if user inputs 'B'
case 'b':
System.out.println("Enter value for side B: ");
data = input.nextInt();
System.out.println("Enter next command: A : value for side A; B : Value for side B; C: Calculate; Q : Quit program.");
s = input.next();
letter = s.charAt(0);
if (letter == 'a')
System.out.println("Enter Value For side a: ");
data1 = input.nextInt();
System.out.println("Enter next command: A : value for side A; B : Value for side B; C: Calculate; Q : Quit program.");
s = input.next();
letter = s.charAt(0);
if (letter == 'c')
sum = data * data + data1 * data1;
int sumsrt1 = (int) Math.sqrt(sum);
System.out.println("The hypotenuse is: " + sumsrt1);
break;
}
System.out.println("Which side would you like to start with A or B");
s = input.next();
letter = s.charAt(0);
}
}
}