【问题标题】:How can i used nested while loop ? in java如何使用嵌套的 while 循环?在java中
【发布时间】:2015-01-12 17:33:50
【问题描述】:

我现在想知道最后一部分如何解决这个程序我不知道我用什么

这是最后一部分


问:


【问题讨论】:

  • 学会缩进你的代码,让代码的结构更清晰。正如所写,它完全不可读。所有 IDE 都使用一个键盘快捷键来完成。
  • @JBNizet 你是什么意思??
  • 如果你不知道缩进代码是什么,那么就大错特错了。在此处查看各种样式的代码:en.wikipedia.org/wiki/Indent_style。所有这些都允许理解代码的结构。您的代码没有,因为行从随机位置开始并且也是随机间隔的。
  • @JBNizet 好的,谢谢。这对我来说是新事物。我是编程初学者:)
  • 应该提供一些部分代码并告诉我们您解决问题的尝试

标签: java while-loop nested nested-loops


【解决方案1】:

在你的外部 while 循环中,你需要这个:

overtime = 0;
if (hoursWorked>8)
    overtime = (hoursWorked-8)*(rateOfPay*1.5) - (hoursWorked-8)*rateOfPay;        // Calculate OVERTIME HOURS

将每次迭代的加班时间设置为零,您必须减去加班时间的正常费率(否则您添加两次)

然后你必须把加班加到工资中:

wages += (hoursWorked * rateOfPay) + overtime;

将所有内容放在一个 do-while 循环中,并在开始时设置 day=1,如下所示:

String choice = "Y";
do {
    day=1;
    while (day <= 5) // on weekday
...

最后:

...
    } //end while loop for weekend.

    choice = JOptionPane.showInputDialog(null, "Do you want to calculate another weeks wages -Yes or No?", "TRY AGIN", JOptionPane.PLAIN_MESSAGE);

} while (choice.equals("Y") || choice.equals("y"));
JOptionPane.showMessageDialog(null, "Pay this week is £" + wages, "Pay", JOptionPane.PLAIN_MESSAGE);

对于你的额外问题-'在开始:

double totalWages = 0;
String choice = "Y";
do {
    day=1;
    wages = 0;
...

最后:

...
    JOptionPane.showMessageDialog(null, "Pay this week is £" + wages, "Pay", JOptionPane.PLAIN_MESSAGE);
    choice = JOptionPane.showInputDialog(null, "Do you want to calculate another weeks wages -Yes or No?", "TRY AGIN", JOptionPane.PLAIN_MESSAGE);
    totalWages += wages;

} while (choice.equals("Y") || choice.equals("y"));
JOptionPane.showMessageDialog(null, "Pay total is £" + totalWages, "Pay", JOptionPane.PLAIN_MESSAGE);

【讨论】:

  • 您可以将“本周工资...”放在循环中,并在外部添加另一个“总工资...”
  • 我想要测试数据和输出应该是这样的(检查我的答案在底部)请!!
【解决方案2】:

这是测试示例

http://im79.gulfup.com/pPMIYk.png

还有我的狂热代码

import javax.swing.*; // Program use Joptionpane
public class HW3 {


   // main method begins execution of Java application
   public static void main(String[] args) {
		
	
	    	// Variable Specifications
	       double rateOfPay =4.5;
	       int hoursWorked=0;
           double wages= 0.0;
           int day=1;
           double overtime=0;
           String choice = "Y";
       	   
       	   
 do {
 
      day=1;	  
      
      while (day <= 5) // on weekday
{ 
        	
         String hours =JOptionPane.showInputDialog(null,"Enter hours worked for day: "+day,
            		       "Work on weekday",JOptionPane.PLAIN_MESSAGE);
         hoursWorked = Integer.parseInt(hours);
        	
        	
         if (hoursWorked>8)
        	 overtime = (hoursWorked-8)*(rateOfPay*1.5); // Calculate OVERTIME HOURS

        	
        
         while (hoursWorked>24) // it's EXTRA It's still again error until the hours be less 24. 
{         
         JOptionPane.showMessageDialog(null,"The hours worked are greater than 24 hours per day ",
        	    			   "error",JOptionPane.PLAIN_MESSAGE);
         hoursWorked=0;
                
         hours =JOptionPane.showInputDialog(null,"Enter hours worked for day: "+day,
                 		 "TRY AGIN",JOptionPane.PLAIN_MESSAGE); 
                 
         hoursWorked = Integer.parseInt(hours);
               
} // end extra while stetment.

        	
        	
        wages += (hoursWorked * rateOfPay)+overtime;
        day = day + 1; 
        	 
       
} // end while loop for weekday.
        


        	 
           
       while (day <= 7) // on weekend
           
{
            	
       String hours =JOptionPane.showInputDialog(null,"Enter hours worked for day on weekend : "+day,
       "work on weekend",JOptionPane.PLAIN_MESSAGE);
       hoursWorked = Integer.parseInt(hours);
           
            	
       while (hoursWorked>24) // it's EXTRA It's still again error until the hours be less 24. 
{         
       JOptionPane.showMessageDialog(null,"The hours worked are greater than 24 hours per day ",
      	    			             "error",JOptionPane.PLAIN_MESSAGE);
       hoursWorked=0;
              
       hours =JOptionPane.showInputDialog(null,"Enter hours worked for day: "+day,
       "TRY AGIN",JOptionPane.PLAIN_MESSAGE); 
               
       hoursWorked = Integer.parseInt(hours);
             
} // end extra while stetment.
            	
             
       wages += (hoursWorked * (rateOfPay*2))+overtime;            	
       day = day + 1; 
            	
 } //end while loop for weekend.


       JOptionPane.showMessageDialog(null,"Pay this week is £"+wages,"Pay",JOptionPane.PLAIN_MESSAGE);
  
       choice = JOptionPane.showInputDialog(null, "Do you want to calculate another weeks wages -Yes or No?",
       "calculate another weeks wages", JOptionPane.PLAIN_MESSAGE);
    
    
    	 
     
  } while (choice.equals("Y") || choice.equals("y"));
 
       JOptionPane.showMessageDialog(null,"Pay this week is £"+wages,"Pay",JOptionPane.PLAIN_MESSAGE);
         
    
    
      
        
     
        	
     }// end method main
        
	}//end class HW3

我怎样才能让它像测试!


本周工资为 324.0 英镑 本周工资为 324.0 英镑 本周工资为 324.0 英镑 支付总额 972.0 英镑


我想放

if (choice.equals("Y") || choice.equals("y"));
    weges=0
 
 } while (choice.equals("Y") || choice.equals("y"));

JOptionPane.showMessageDialog(null,"Pay this week is £"+wages,"Pay",JOptionPane.PLAIN_MESSAGE);
         
    
    
      
      
        	
     }// end method main
        
	}//end class HW3

怎么才能放总!?

【讨论】:

  • @NerosE 感谢您的帮助。 大心脏 :)
【解决方案3】:

您已经有两个循环,即:

while (day <= 5) { //for week day
    //blah blah
}
while (day <= 7) { //for weekend
    //blah blah
}

所以如果你想等待用户输入,你需要这样:

do {

    //your two while loops
    //ask user if he/she wish to continue y/N just like you are doing to get hours and save it to a variable say userInput
    char userInput = .....;
} while (userInput == 'Y' || userInput == 'y');

【讨论】:

  • 它不起作用!我想在 joptionpan 中使用它 __ String userInput = JOptionPane.showInputDialog(null,"你想再计算一周的工资 - 是还是否?");___ 我如何将字符串转换为 char
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-20
  • 2011-06-25
  • 1970-01-01
相关资源
最近更新 更多