如何解决Java中的代数程序无法正常工作
我制作了一个程序,该程序应在选择由于某种原因而无效的代数方程后显示一个方程。 我尝试更改扫描仪名称,但是在选择表达式时它只会显示错误选择。 如果您能帮助我,我将非常感谢。 这是代码:
/** 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");
}
}
}```
解决方法
您需要将大小写更改为case 1
和case 2
,而不是case '1'
和case '2'
。
请注意,'1'
和'2'
是char
的文字;而不是int
文字。 '1'
的ASCII值为49
,而'2'
的ASCII值为50
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。