0

您好,我正在上 java 课,我有这个程序作为作业。我试图找出谁来设置值并使用 Scanner 类返回计算。我是java的初学者(使用任何comp.语言)。如果你能帮我解决这个问题。我到处找,但找不到解决方案。

/**
* Write a description of class BookclubTester here.
*
* @author (your name)
* @version (a version number or a date)
*/
    import java.util.Scanner;  

    public class BookclubTester {

        public BookclubTester() {
            String  studentName;
        } 
 
        public static void main (String [] args) {
 
            int books;  
            int count = 0;
            while (count < 5) {
                Scanner input = new Scanner(System.in);

                System.out.println("Please provided your name :" );
                String studentName = input.nextLine();
    
                Bookclub rewardsProgram = new Bookclub();
                System.out.println("Please provide the numbers of books purchase for the  month " );
                books = input.nextInt();
     
                rewardsProgram.setBooks(books);
   
                System.out.println(rewardsProgram.getRewardPoints());
                count++;
            }
        }
   }

这是课

/**
*
* @author
*/
public class Bookclub {

    private  String studentName;
    private int books = 0;
    private  int rewardPoints;

    public Bookclub () {
 
    }

    public int getBooks() {
        return books;
    }

    public void setBooks(int books) {
        this.books = books;
    }

    public int getCalculateRewardPoints (){
        return rewardPoints;
    }

    public int getRewardPoints() {
        return rewardPoints;
    }

    public void setCalculateRewardPoints (int rewardPoints) {
        this.rewardPoints = rewardPoints;
        if (books == 0) {
            rewardPoints = 0;
            System.out.println("Your points are:" + rewardPoints);
       } else if (books == 1) {
            rewardPoints = 5;
       }else if (books == 2) {
            rewardPoints = 15;//
       }else if(books == 3) {
            rewardPoints = 30;
       }else if(books == 4) {
            rewardPoints = 60;
       } 
    }

    public void setRewardPoints(int rewardPoints) {
        this.rewardPoints = rewardPoints;
            if(rewardPoints == 0) {
                rewardPoints = 0;
            }else if(rewardPoints ==5) {
                System.out.println("Your points are : " + rewardPoints);
            }else if(rewardPoints == 15) {
                System.out.println("Your points are:" + rewardPoints);
            }else if(rewardPoints == 30) { 
                System.out.println("Your points are:" + rewardPoints);
            }else if(rewardPoints == 60) { 
                System.out.println("Your points are:" + rewardPoints);
            }  
        }
}
4

3 回答 3

0

您的代码中有一点混乱,但对于初学者来说这是正常的:)

首先,在您的主类中,您打印rewardsProgram.getRewardPoints()的只是简单地返回rewardPointsnull!

当您在setBooks方法中设置书籍时,您必须调用第二种方法来计算您的奖励积分,在您的情况下,它是setRewardsPoints. 在此方法中,您必须使用变量books来获取积分,您可以使用以下内容:

switch(books){
  case 0:
   rewardsPoints=0; break;
  case 1:
   rewardsPoints=5; break;
  ...
}

这就是你应该如何计算你的分数。要打印奖励积分,只需执行以下操作:

System.out.println(rewardsProgram.getRewardPoints());

你还需要清除一点你的类,有些方法是没用的!这是一种简单的方法来做你需要的事情。

于 2020-10-18T12:58:38.383 回答
0

您应该解决代码中存在的以下问题:

  1. String studentName;构造函数内部的声明BookclubTester(){}是无用的。
  2. 您既没有定义 setter 和 getter 方法,studentName也没有将其设置到对象中rewardsProgram
  3. Scanner在循环外实例化,while Scanner input = new Scanner(System.in);应该在while循环之外。您可以重用在循环Scanner外实例化的相同对象while
  4. 我还建议您使用Integer.parseInt(input.nextLine())而不是input.nextInt()避免此处描述的问题。
  5. 你还没有设置rewardPoints成对象,rewardsProgram. 理想情况下,应该调用计算/设置奖励积分的方法,setBooks因为奖励积分是基于书籍的数量。
  6. if (books >= 4)当书籍数量超过 时,奖励积分应该有一个条件(例如) 4

下面给出了包含这些建议的代码:

import java.util.Scanner;

class Bookclub {

    private String studentName;
    private int books = 0;
    private int rewardPoints;

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public int getBooks() {
        return books;
    }

    public void setBooks(int books) {
        this.books = books;
        setRewardPoints(books);
    }

    public int getCalculateRewardPoints() {
        return rewardPoints;
    }

    public int getRewardPoints() {
        return rewardPoints;
    }

    public void setRewardPoints(int books) {
        if (books == 0) {
            rewardPoints = 0;
        } else if (books == 1) {
            rewardPoints = 5;
        } else if (books == 2) {
            rewardPoints = 15;//
        } else if (books == 3) {
            rewardPoints = 30;
        } else if (books >= 4) {
            rewardPoints = 60;
        }
    }
}

public class Main {

    public static void main(String[] args) {
        int books;
        int count = 0;
        Scanner input = new Scanner(System.in);

        while (count < 5) {
            Bookclub rewardsProgram = new Bookclub();

            System.out.print("Please provided your name: ");
            String studentName = input.nextLine();
            rewardsProgram.setStudentName(studentName);

            System.out.print("Please provide the numbers of books purchase for the  month: ");
            books = Integer.parseInt(input.nextLine());
            rewardsProgram.setBooks(books);

            System.out.println("Reward points: " + rewardsProgram.getRewardPoints());
            count++;
        }
    }
}

示例运行:

Please provided your name: Test1
Please provide the numbers of books purchase for the  month: 2
Reward points: 15
Please provided your name: Test2
Please provide the numbers of books purchase for the  month: 1
Reward points: 5
Please provided your name: Test3
Please provide the numbers of books purchase for the  month: 7
Reward points: 60
Please provided your name: Test4
Please provide the numbers of books purchase for the  month: 0
Reward points: 0
Please provided your name: Test5
Please provide the numbers of books purchase for the  month: 4
Reward points: 60

您仍然可以考虑许多验证(例如检查书籍数量是否为负数),我希望这些建议将帮助您针对这些事情设计程序。

于 2020-10-18T14:03:12.110 回答
0
public class BookClubMember {

    private final String name;
    private int books;
    private int rewardPoints;

    public BookClubMember(String name, int numberOfBooks) {
            this.name = name;
            setBooks(numberOfBooks);
    }

    public BookClubMember(String name) {
            this(name, 0);

    }

    public final String getName() {
            return name;
    }

    public final int getBooks() {
            return books;
    }

    public final void setBooks(int numberOfBooks) {
            int oldValue = books;
            if (numberOfBooks <= 0) {
                books = 0;
            } else {
                books = numberOfBooks;
            }
            if (oldValue != books) {
                // update your points if they have changed.
                rewardPoints = calculateRewardPoints(books);
            }
    }

        
    public final int getRewardPoints() {
            return rewardPoints;
    }

        /**
         * This can be overridden in the future if you ever wanted to extend
         * this class and make a "Platinum Member"... etc where they get more points
         * per book, or they are on a linear scale, etc etc.
         * 
         *
         * @param bookCount
         * @return
         */
        protected int calculateRewardPoints(int bookCount) {
            switch (bookCount) {
                case 0:
                    return 0;
                case 1:
                    return 5;
                case 2:
                    return 15;
                case 3:
                    return 30;
                default:
                    return 60;
            }
        }
    }

而你的主要方法

 public static void main(String[] args) {
    
            // so you need to work in a scanner object here to collect a name, and number of books.
            // once you have both you can create a new BookClubMember object.
            // I'm cheating and manually putting them in :)
            BookClubMember member = new BookClubMember("Johnny", 5);
            System.out.println(member.getName() + " has " + member.getRewardPoints() + " points " + " for " + member.getBooks() + " books");
        }

输出:

约翰尼 5 本书得 60 分

于 2020-10-18T14:04:05.817 回答