java代码没有像我期待的那样工作

时间:2016-12-01 02:32:58

标签: java loops while-loop storage

我真的很努力让我的java代码工作。我编写了一个程序,用于确定用户输入后每个分数的等级,并找出最大和最小分数。该程序成功地确定了哪个分数属于哪个等级,但是一旦我实现了一段代码,试图找到分数中最大的分数,它似乎不起作用,我不确定它是什么!

这是代码......

UICollectionViewFlowLayout *layout     = [[UICollectionViewFlowLayout alloc] init];
[layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];

layout.scrollDirection                 = UICollectionViewScrollDirectionHorizontal;
_myCollectionView.collectionViewLayout = layout;
_myCollectionView.autoresizingMask     = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;  

谢谢!

3 个答案:

答案 0 :(得分:2)

这可能是一个练习,所以会给出这个想法。 你的while循环似乎很好,只需在它之前声明两个变量。 一个用于保持最低等级,用最低编号初始化,另一个用于最高等级的最低等级。

score = in.nextInt();

if ( score > highest  ) { highest = score;}
if ( score < lowest ) { lowest = score }
祝你好运。

答案 1 :(得分:0)

问题很可能是每次while循环运行时都声明变量input。相反,声明变量如下:

double input;

并在while循环之前完成。然后在while循环中用以下代码替换该行:

input = in.nextDouble();

如果这不是问题,那么可能就是你如何布置代码。以while (score>0)开头的第二个while循环应该是你有int score = in.nextInt()行的地方。在第一个while循环之后放置它意味着它在while循环之后执行。

作为旁注,像Agrade这样的常见变量应该写在camelCase中,其中第一个字母是小写,下一个单词的字母是大写。这会使你的变量

int aGrade = 0;
int bGrade = 0;

等等。这只是正确的形式,不应该影响你的程序。此外,您可能应该在程序顶部声明得分和输入,因为您在循环中使用它们。这也是良好的形式和组织。它看起来像这样:

int score;
double input;

答案 2 :(得分:-1)

检查对代码的评论以便理解。

MainClass:

import java.util.Scanner;

public class Grade
{
    public static void main ( String [ ] args )
    {
        /**
         * This gradesCount represent the A,B,C,D,E,F counts
         */
        int[] gradesCount = {0,0,0,0,0,0}; 
        double inputScore = 0;  
        double bestScore = 0;
        int total = 0;

        @SuppressWarnings ( "resource" )
        Scanner in = new Scanner(System.in);

        System.out.println ( "If you want to end, enter a negative number" );
        System.out.print("Enter score: ");

        do
        {
            inputScore = in.nextDouble ( );
            //validation for the first iteration
            if(inputScore < 0 || inputScore > 100) break;
            System.out.print("Enter score: ");
            //Add to corresponding grade count
            if(inputScore>=70 && inputScore<=100) gradesCount[0]++;
            if(inputScore>=60 && inputScore<=69) gradesCount[1]++;
            if(inputScore>=50 && inputScore<=59) gradesCount[2]++;
            if(inputScore>=40 && inputScore<=49) gradesCount[3]++;
            if(inputScore>=0 && inputScore<=39) gradesCount[4]++;
            //Add one to total
            total++;
            //check best score
            if(inputScore > bestScore) bestScore = inputScore;
        }//This pattern check if its number between 0-100
        while ( in.hasNext ( "[0-9][0-9]?|100" ) );



        System.out.println ( "Negative number or not valid input. Exited." );

        System.out.println("Total number of grades :"+ total);
        System.out.println("The best score :"+ bestScore);
        System.out.println("The number of As :"+ gradesCount[0]);
        System.out.println("The number of Bs :"+ gradesCount[1]);
        System.out.println("The number of Cs :"+ gradesCount[2]);
        System.out.println("The number of Ds :"+ gradesCount[3]);
        System.out.println("The number of Fs :"+ gradesCount[4]);


    } 
}

输入/输出:

If you want to end, enter a negative number
Enter score: 5
Enter score: 10
Enter score: 15
Enter score: 20
Enter score: 33
Enter score: 99
Enter score: 100
Enter score: 44
Enter score: -3
Negative number or not valid input. Exited.
Total number of grades :8
The best score :100.0
The number of As :2
The number of Bs :0
The number of Cs :0
The number of Ds :1
The number of Fs :5

文档: