首先,这是一项家庭作业。我应该使用switch命令创建一个程序。它要求用户输入3个整数,然后输入1-5的整数,用于平均,最大,最小,总计和退出的五种情况。我已经完成了这个程序,首先有一个用户输入3个整数,然后创建开关“菜单”结构,在每个案例中,我试图为每个算术函数创建数学。
我的错误消息是“找不到符号”,这是一个广泛而模糊的错误消息要发布。我在Google网站上搜索或找到的所有内容似乎与我的程序缺失的特定符号无关,和/或本质上可以是印刷的。所以,不幸的是,我不得不创建另一个关于此的线程。
我的程序找不到平均值,最大值,最小值等符号。我相信我可能已经完成了我的算术算法错误,并且它们要么没有正确初始化,要么根本没有初始化,或者我需要在使用切换命令之前将它们设置为零。
我需要发布代码,以便您可以看到我做了什么:
>public static void main( String args[] )
{
// Attempting to initialize the 3 integers AND the user-input early on
int n1, n2, n3, n4;
Scanner sc = new Scanner( System.in);
while ( true )
{
System.out.println ( "Please give three integers separated by spaces: " );
n1 = sc.nextInt();
n2 = sc.nextInt();
n3 = sc.nextInt();
// building a menu - hoping integers aboive will work in Switch command
System.out.println( "\n****** Analysis Menu ******" );
System.out.println( "1: Average" );
System.out.println( "2: Maximum" );
System.out.println( "3: Minimum" );
System.out.println( "4: Total" );
System.out.println( "5: Exit" );
System.out.println( "*******************\n" );
n4 = sc.nextInt(); // get the user selection
if ( n4 == 1 )
{
Average = n1 + n2 + n3 / 3;
System.out.print( "Average = " + Average );
}
else if ( n4 == 2 )
{
max = n1;
if ( max < n2 ) max = n2;
if ( max < n3 ) max = n3;
System.out.println( "Maximum = " + max );
}
else if ( n4 == 3 )
{
min = n1;
if ( min > n2) min = n3;
if ( min > n3) min = n2;
System.out.print( "Minimum = " + min );
}
else if ( n4 == 4 )
{
total = sum;
System.out.print( "total = " + total );
}
else if ( n4 == 5 )
{
System.out.println( "You have selected Exit." );
break;
}
else
{
System.out.println( "No such selection exists." );
错误消息表明他们无法找到每个总数,最小值,最大值,平均值和总和。所以我不确定我是否需要以某种方式初始化它们,将它们设置为0,或者甚至在这种情况下如何做到这一点。
答案 0 :(得分:1)
上述代码中存在多个问题: 1.未定义平均,最小,最大变量。 你平均逻辑不对。 你缺少支撑杆。
我试图修改这些,这就是我想出来的。
public static void main( String args[] )
{
// Attempting to initialize the 3 integers AND the user-input early on
int n1, n2, n3, n4;
int Average, max, min, total;
Scanner sc = new Scanner( System.in);
while ( true )
{
System.out.println ( "Please give three integers separated by spaces: " );
n1 = sc.nextInt();
n2 = sc.nextInt();
n3 = sc.nextInt();
// building a menu - hoping integers aboive will work in Switch command
System.out.println( "\n****** Analysis Menu ******" );
System.out.println( "1: Average" );
System.out.println( "2: Maximum" );
System.out.println( "3: Minimum" );
System.out.println( "4: Total" );
System.out.println( "5: Exit" );
System.out.println( "*******************\n" );
n4 = sc.nextInt(); // get the user selection
if ( n4 == 1 )
{
Average = (n1 + n2 + n3) / 3;
System.out.print( "Average = " + Average );
}
else if ( n4 == 2 )
{
max = n1;
if ( max < n2 ) max = n2;
if ( max < n3 ) max = n3;
System.out.println( "Maximum = " + max );
}
else if ( n4 == 3 )
{
min = n1;
if ( min > n2) min = n3;
if ( min > n3) min = n2;
System.out.print( "Minimum = " + min );
}
else if ( n4 == 4 )
{
total = n1 + n2 + n3;
System.out.print( "total = " + total );
}
else if ( n4 == 5 )
{
System.out.println( "You have selected Exit." );
break;
}
else
{
System.out.println( "No such selection exists." );
}
}
}
答案 1 :(得分:0)
您需要将Average
,min
,max
,total
定义为变量,就像n1,..,n4
一样。我的意思是
decimal Average;
顺便说一句,最好避免像这样命名变量,例如minValue
或maxValue
更适合命名。