【问题标题】:Counter in while loop does not increasewhile循环中的计数器不增加
【发布时间】:2014-06-08 15:16:56
【问题描述】:

我需要用 Java 开发奖学金申请。我需要在输出中打印成功的应用程序。

如果我换第二个人,为什么这段代码中的计数器不会增加?

import java.util.Scanner;
import java.util.LinkedList;

public class Main
{
  public static void main(String [] args)
  {
    Scanner input = new Scanner(System.in);
    Application a = new Application();
    LinkedList lList = new LinkedList();
    boolean bEligible = false;
    int scholarship;
    int scholarshipAmount = 500000;
    int setiStudyLevel;
    int counter;

    while(scholarshipAmount != 0)
    {
      counter = 1;
      System.out.println("");    
      System.out.println("**Welcome to scholarship  applications  offered  by  Smart Foundation**");    
      System.out.println("");
      System.out.println("");
      System.out.println("Please Enter Your Name");
      a.setsName(input.next());

      System.out.println("1–Pre Diploma, 2–Diploma, 3–Degree");
      System.out.println("Please Enter Your Study Level");
      a.setiStudyLevel(input.nextInt());

      System.out.println("Please Enter Your Personality Score");
      a.setiPersonalityScore(input.nextInt());

      System.out.println("Please Enter Your Parents Income");
      a.setdParentsIncome(input.nextDouble());

      String sName = a.getsName();
      int iStudyLevel = a.getiStudyLevel();
      int iPersonalityScore = a.getiPersonalityScore();
      double dParentsIncome = a.getdParentsIncome();

      if (iStudyLevel == 1)
      {
        scholarship = 15000;
        scholarshipAmount = scholarshipAmount - scholarship;
      }
      else if (iStudyLevel == 2)
      {
        scholarship = 50000;
        scholarshipAmount = scholarshipAmount - scholarship;
      }
      else if (iStudyLevel == 3)
      {
        scholarship = 500000;
        scholarshipAmount = scholarshipAmount - scholarship;
      }

   System.out.println("");
   System.out.println("");
   System.out.println("*****Result*****"); 
   System.out.println("Name:"+sName);
   System.out.println("1–Pre Diploma, 2–Diploma, 3–Degree");
   System.out.println("Study Level:"+iStudyLevel);
   System.out.println("Personality Score:"+iPersonalityScore);
   System.out.println("Parents Income:"+dParentsIncome);

    if(iPersonalityScore <=90)
    {
        if(dParentsIncome >=3000)
        {
            System.out.println("YOU ARE NOT ELIGIBLE FOR SCHOLARSHIP ");
        }
        else
        {
            System.out.println("YOU ARE ELIGIBLE FOR SCHOLARSHIP ");
            bEligible = true;
        }
    }
    else
    {
        System.out.println("YOU ARE ELIGIBLE FOR SCHOLARSHIP ");
    }

   System.out.println("");

   System.out.println( "RM"+(scholarshipAmount)+" left!!");
   System.out.println("");

   if (scholarshipAmount <= 0)
   {
       System.out.println(" Scholarship Quota is Full!");
   }

   counter++;

   System.out.println(counter);
   }
}
}

【问题讨论】:

    标签: java while-loop counter


    【解决方案1】:

    问题是您在每个循环中将计数器重置为 1

    改变这个:

    while(scholarshipAmount != 0)
    {
    counter = 1;
    

    到这里

    int counter = 1;//or maybe 0
    while(scholarshipAmount != 0)
    {
    

    【讨论】:

    • 不客气。对于您得到正确答案的任何问题,不要忘记将答案标记为已接受。
    【解决方案2】:

    你的循环应该是,

    while(scholarshipAmount > 0)
        {
          //remaining code
    

    【讨论】:

    • 感谢 pranit 我下次应该考虑使用这个
    猜你喜欢
    • 2017-06-19
    • 1970-01-01
    • 1970-01-01
    • 2016-04-02
    • 2016-02-27
    • 2012-11-26
    • 1970-01-01
    • 2017-07-15
    • 1970-01-01
    相关资源
    最近更新 更多