英文:
An algebra program in java is not working correctly
问题
import java.util.*;
class F {
public static void main(String a[]) {
Scanner in = new Scanner(System.in);
System.out.println("执行代数程序:要键入的编号:");
System.out.println(" (i) 3X+4Y+Z 1 ");
System.out.println(" (ii) 3A^2+4B^3+3C 2 ");
int ch = in.nextInt();
switch (ch) {
// Program 1
case 1:
Scanner sc = new Scanner(System.in);
System.out.println("输入 X 的数值...");
int X = sc.nextInt();
System.out.println("输入 Y 的数值...");
int Y = sc.nextInt();
System.out.println("输入 Z 的数值...");
int Z = sc.nextInt();
int sum = (3 * X) + (4 * Y) + Z;
System.out.println(" ");
System.out.println("X 的数值为:" + X);
System.out.println("Y 的数值为:" + Y);
System.out.println("Z 的数值为:" + Z);
System.out.println("3X+4Y+Z 的值为:" + sum);
break;
// Program 2
case 2:
Scanner hi = new Scanner(System.in);
System.out.println("输入 A 的数值...");
double A = hi.nextDouble();
System.out.println("输入 B 的数值...");
double B = hi.nextDouble();
System.out.println("输入 C 的数值...");
double C = hi.nextDouble();
double pro = 3 * Math.pow(A, 2) + 4 * Math.pow(B, 3) + 3 * C;
int isum = (int) pro;
System.out.println(" ");
System.out.println("A 的数值为:" + A);
System.out.println("B 的数值为:" + B);
System.out.println("C 的数值为:" + C);
System.out.println("3A^2+4B^3+3C 的值为:" + isum);
break;
// Else
default:
System.out.println("错误的选择");
}
}
}
英文:
I made a program which should display an equation after choosing a type of algebra equation which doesn't works for some reason.
I have tried changing the scanners name but it just displays Wrong choice when an expression is chosen.
I would really appreciate it if you can help me thank you.
This is the code:
/** WAP using switch case to print the following :
1. 3X+4Y+Z
2. 3A^2+4B^3+3C*/
import java.util.*;
class F
{
public static void main(String a[])
{
Scanner in = new Scanner(System.in);
System.out.println("Algebra Program executed : No. to be typed: ");
System.out.println(" (i) 3X+4Y+Z 1 ");
System.out.println(" (ii) 3A^2+4B^3+3C 2 ");
int ch = in.nextInt();
switch(ch)
{
//Program 1
case '1': Scanner sc = new Scanner(System.in);
System.out.println("Type a number for X...");
int X = sc.nextInt();
System.out.println("Type a number for Y...");
int Y = sc.nextInt();
System.out.println("Type a number for Z...");
int Z = sc.nextInt();
int sum = (3*X)+(4*Y)+Z;
System.out.println(" ");
System.out.println("Value of X is "+X);
System.out.println("Value of Y is "+Y);
System.out.println("Value of Z is "+Z);
System.out.println("Value of 3X+4Y+Z is "+sum);
break;
//Program 2
case '2': Scanner hi = new Scanner(System.in);
System.out.println("Type a number for A...");
double A = hi.nextDouble();
System.out.println("Type a number for B...");
double B = hi.nextDouble();
System.out.println("Type a number for C...");
double C = hi.nextDouble();
double pro = 3*Math.pow(A, 2) + 4*Math.pow(B, 3) + 3*C;
int isum = (int) pro;
System.out.println(" ");
System.out.println("Value of A is "+A);
System.out.println("Value of B is "+B);
System.out.println("Value of C is "+C);
System.out.println("Value of 3A^2+4B^3+3C is "+isum);
break;
//Else
default: System.out.println("Wrong Choice");
}
}
}```
</details>
# 答案1
**得分**: 4
你需要将情况更改为 `case 1` 和 `case 2`,而不是 `case '1'` 和 `case '2'`。
请注意,`'1'` 和 `'2'` 是 `char` 字面值;不是 `int` 字面值。`'1'` 的 ASCII 值是 `49`,`'2'` 的 ASCII 值是 `50`。
<details>
<summary>英文:</summary>
You need to change the case to `case 1` and `case 2` instead of `case '1'` and `case '2'`.
Note that `'1'` and `'2'` are `char` literals; not the `int` literals. the ASCII value of `'1'` is `49` and that of `'2'` is `50`.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论