import java.util.Scanner;
public class Switch {
private static void case1() {
Scanner input = new Scanner(System.in);
System.out.println("Enter side A");
String a = input.next();
System.out.println("Enter side B");
String b = input.next();
double number1 = Double.parseDouble(a);
double number2 = Double.parseDouble(b);
double A2 = number1 * number1;
double B2 = number2 * number2;
System.out.println(A2);
System.out.println(B2);
double Csq = A2 + B2;
double C = Math.sqrt(Csq);
System.out.println("C equals " + C);
input.close();
}
private static void case2() {
Scanner input = new Scanner(System.in);
System.out.println("Enter side A");
String a = input.next();
System.out.println("Enter side C");
String c = input.next();
double number1 = Double.parseDouble(a);
double number2 = Double.parseDouble(c);
double A2 = number1 * number1;
double C2 = number2 * number2;
System.out.println(A2);
System.out.println(C2);
double b1 = C2 - A2;
double B = Math.sqrt(b1);
System.out.println("B equals " + B);
input.close();
}
private static void case3() {
Scanner input = new Scanner(System.in);
System.out.println("Enter side B");
String b = input.next();
System.out.println("Enter side C");
String c = input.next();
double number1 = Double.parseDouble(b);
double number2 = Double.parseDouble(c);
double B2 = number1 * number1;
double C2 = number2 * number2;
System.out.println(B2);
System.out.println(C2);
double a1 = C2 - B2;
double A = Math.sqrt(a1);
System.out.println("A equals " + A);
input.close();
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String cont = "y";
do {
try{
System.out.println("Welcome to the Pythagorean Theorm Program!!");
System.out.println("");
System.out.println("Choose one of the following:");
System.out.println("");
System.out.println("1. A2 + B2 = X");
System.out.println("2. A2 + X = C2");
System.out.println("3. X + B2 = C2");
int choice = input.nextInt();
switch (choice) {
case 1: case1();
break;
case 2: case2();
break;
case 3: case3();
break;
}
}
catch(Exception e) {
System.out.println("error");
}
System.out.println("Do you want to continue?");
String more = input.next();
} while (more.equals(cont));
}
}
我需要在while循环之前输入一个用户输入,这样才能知道程序是否要继续。问题是,while内部条件越多,之前看不到扫描仪输入一行。有什么建议吗?
答案 0 :(得分:0)
将字符串更多声明放在while循环之外:
`public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String cont = "y";
String more = null;
do {
try{
System.out.println("Welcome to the Pythagorean Theorm Program!!");
System.out.println("");
System.out.println("Choose one of the following:");
System.out.println("");
System.out.println("1. A2 + B2 = X");
System.out.println("2. A2 + X = C2");
System.out.println("3. X + B2 = C2");
int choice = input.nextInt();
switch (choice) {
case 1: case1();
break;
case 2: case2();
break;
case 3: case3();
break;
}
}
catch(Exception e) {
System.out.println("error");
}
System.out.println("Do you want to continue?");
more = input.next();
} while (more.equals(cont));
}`
答案 1 :(得分:0)
变量more
仅在do while循环内的范围内。一种解决方案是在循环外部声明更多,并仅更新其内部值。
喜欢这个......
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String cont = "y";
String more = null;
do {
.
.// code here
.
System.out.println("Do you want to continue?");
more = input.next();
} while (more.equals(cont));
这是因为在do-while构造中声明的任何变量只在同一个循环中的范围内,不能在循环外部或循环条件中访问。
在循环外部和main内声明变量可以在main方法内的任何位置访问变量。
答案 2 :(得分:0)
这里的问题是变量范围,你在do语句中更多地定义了String,然后在它之外有while条件,为了修复它你需要将范围改为local到main函数来执行此操作只需在执行
之前将其添加到代码中 public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String cont = "y";
String more = null;
do {
// rest of code
你还需要摆脱“String”关键字,以便在你的do中定义更多的数据类型,但应该是它。
虽然如果你的目标是重复程序,如果他们需要再次使用它,为什么不在while循环中封装整个主函数,而条件依赖于用户输入结束时循环例如
boolean more = True;
While(more = True){
// your current code
// then check to see if they want to go again, if not , change more to false
}
我希望这会有所帮助