我创建了一个加班计算器,如果用户输入的时间超过49小时,则会向用户发出警告,然后循环回到用户输入小时以使用有效时间的位置。
如果有人输入正确的小时并想发出另一个查询,如何在循环内发出循环?因为它目前已在循环中设置。
特异性:
System.out.println ("Would you like to view another account? Y/N: ");
inputLine.next().charAt(0);
以下是诊断的完整代码:
import java.util.Scanner;
public class Overtime
{
public static void main()
{
Scanner inputLine = new Scanner( System.in );
String staffName;
int i = 1;
double hoursWorked = 0, hourlyRate = 15, totalPay = 0, nationalInsurance = 0, tax = 0, netPay = 0, overtime = 0;
System.out.print( "Enter employee name: " );
staffName = inputLine.nextLine();
do
{
System.out.print( "Enter number of hours worked: " );
hoursWorked = inputLine.nextFloat();
if( hoursWorked <= 36 )
{
totalPay = (hourlyRate * hoursWorked);
}
else if( hoursWorked >= 37 && hoursWorked <= 40 )
{
totalPay = (hourlyRate * 36) + (hoursWorked - 36) * (hourlyRate * 1.5);
}
else //(hoursWorked > 41 )
{
totalPay = (hourlyRate * 36) + (41 - 36) * (hourlyRate * 1.5) + (hoursWorked - 41) * (hourlyRate * 2);
}
if( hoursWorked >= 37 && hoursWorked <= 40 )
{
overtime = (hoursWorked - 36) * (hourlyRate * 1.5);
}
else if( hoursWorked > 41 )
{
overtime = (41 - 36) * (hourlyRate * 1.5) + (hoursWorked - 41) * (hourlyRate * 2);
}
if( totalPay > 155 )
{
nationalInsurance = (totalPay * 0.12);
}
tax = (totalPay * 0.20);
netPay = (totalPay - tax - nationalInsurance);
if( hoursWorked >= 49 )
{
System.out.println( "You are not legally allowed to work over 48 hours! " );
}
else
{
System.out.println( "***********************" );
System.out.println( "Employee: " + staffName );
System.out.println( "Total Hours Worked: " + hoursWorked );
System.out.println( "Overtime Pay: " + overtime );
System.out.println( "Net Pay: " + totalPay );
System.out.println( "Tax: " + tax );
System.out.println(
"National insurance: " + (nationalInsurance = Math.round( nationalInsurance * 100.00 ) / 100.00) );
System.out.println( "Net Pay" + netPay );
i++;
System.out.println( "Would you like to view another account? Y/N: " );
inputLine.next().charAt( 0 );
//System.out.println ("Overtime Pay" + (hoursWorked - 36) * (hourlyRate * 1.5) + (hoursWorked - 41) * (hourlyRate * 2));
//System.out.println ("Total Deductions " + (totalPay * (20/100) + (totalPay * (4/100))));
}
}
while( i == 1 );
}
}
答案 0 :(得分:0)
您可能应该查看对inputLine.next()的调用结果.charAt(0);
你可以将你在这里的全部内容放在另一个循环中,你可以根据该调用的输出作出条件。
但是,我强烈建议您重新构建此代码,以便在您阅读它时使其符合逻辑,而不是将其全部用于一个大方法调用。
答案 1 :(得分:0)
我建议您查看以下代码并将其与您自己的代码进行比较。从这里你可以看到差异,循环更有意义。如果用户不再想要输入更多帐户,则按N
将退出循环。
public static void main( String args[] )
{
Scanner inputLine = new Scanner( System.in );
String staffName, quit;
double hoursWorked = 0, hourlyRate = 15, totalPay = 0, nationalInsurance = 0, tax = 0, netPay = 0, overtime = 0;
do
{
System.out.print( "Enter employee name: " );
staffName = inputLine.next();
System.out.print( "Enter number of hours worked: " );
hoursWorked = inputLine.nextFloat();
/**
* All other logic.
*/
if( hoursWorked >= 49 )
{
System.out.println( "You are not legally allowed to work over 48 hours! " );
}
else
{
System.out.println( "The output" );
}
System.out.println ("Would you like to create another account? Y/N: ");
quit = inputLine.next();
}
while( !quit.equalsIgnoreCase( "N" ) );
}
答案 2 :(得分:0)
您可以将do块的列表行更改为:
if(inputLine.nextLine().charAt(0)!='Y') i=0;
因此,如果用户输入no(或其他内容),则i的值将更改为0并且循环将中断。
另一种好的方法是使用i作为char变量,并且最初将其值设置为&#39; Y&#39;然后使用
i=inputLine.nextLine().charAt(0);
和
while(i=='Y'||i=='y');
此外,代码不应该做过多和不必要的事情,这将是一个糟糕的设计! 如果小时数超过48小时则无需计算任何内容,它应该循环回到用户再次输入小时数的位置。因此,在接受输入后检查是否超过48小时。如果它小于49,那么在一个块中进行所有计算并打印并询问用户是否想要输入另一个输入。如果小时> 48,则打印出无效的消息,并再次询问用户输入。
这里是代码
import java.util.*;
public class Overtime
{
public static void main ()
{
Scanner inputLine = new Scanner(System.in);
String staffName;
char i = 'Y';
double hoursWorked = 0, hourlyRate = 15, totalPay=0, nationalInsurance=0, tax=0, netPay=0, overtime=0;
System.out.print ("Enter employee name: ");
staffName = inputLine.nextLine();
do{
System.out.print ("Enter number of hours worked: ");
hoursWorked = inputLine.nextFloat();
if (hoursWorked >= 49 )
{
System.out.println ("You are not legally allowed to work over 48 hours! ");
}
else
{
if ( hoursWorked <= 36 )
{
totalPay = (hourlyRate * hoursWorked);
}
else if ( hoursWorked >= 37 && hoursWorked <= 40)
{
totalPay = (hourlyRate * 36) + (hoursWorked - 36) * (hourlyRate * 1.5);
}
else //(hoursWorked > 41 )
{
totalPay = (hourlyRate * 36) + (41-36) * (hourlyRate * 1.5) + (hoursWorked - 41) * (hourlyRate * 2);
}
if ( hoursWorked >= 37 && hoursWorked <= 40)
{
overtime = (hoursWorked - 36) * (hourlyRate * 1.5);
}
else if ( hoursWorked > 41)
{
overtime = (41-36) * (hourlyRate * 1.5) + (hoursWorked - 41) * (hourlyRate *2);
}
if (totalPay > 155 )
{
nationalInsurance = (totalPay * 0.12);
}
tax = (totalPay * 0.20);
netPay = (totalPay - tax - nationalInsurance);
System.out.println ( "***********************");
System.out.println ("Employee: " + staffName);
System.out.println ("Total Hours Worked: " + hoursWorked);
System.out.println ("Overtime Pay: " + overtime);
System.out.println ("Net Pay: " + totalPay);
System.out.println ("Tax: " + tax);
System.out.println ("National insurance: " + (nationalInsurance = Math.round(nationalInsurance * 100.00 )/ 100.00));
System.out.println ("Net Pay" + netPay);
i++;
System.out.println ("Would you like to view another account? Y/N: ");
i=inputLine.nextLine().charAt(0);
//System.out.println ("Overtime Pay" + (hoursWorked - 36) * (hourlyRate * 1.5) + (hoursWorked - 41) * (hourlyRate * 2));
//System.out.println ("Total Deductions " + (totalPay * (20/100) + (totalPay * (4/100))));
}
}
while(i == 'Y'||i=='y');
}