试图获得奖励积分,但我无法设置书籍的价值
trying to get the reward points and I can't set the value of Books
你好,我正在学习 java class 并且我将这个程序作为作业。我试图找出谁来设置值和 return 使用 Scanner class 的计算。我是 java(任何语言)的初学者。如果你能帮我弄清楚这个问题。我到处都找了,但找不到解决方案。
/**
* 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++;
}
}
}
这是class
/**
*
* @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);
}
}
}
你的代码有点混乱,但对于初学者来说这是正常的:)
首先,在您的主要 class 中,您正在打印 rewardsProgram.getRewardPoints()
只是简单地 returns rewardPoints
为空!
当您在 setBooks
方法中设置书籍时,您必须调用第二个方法来计算您的 rewardsPoints,在您的例子中是 setRewardsPoints
。在这个方法中,你必须使用你的变量 books
来获得你的分数,你可以使用类似的东西:
switch(books){
case 0:
rewardsPoints=0; break;
case 1:
rewardsPoints=5; break;
...
}
这就是您计算积分的方式。要打印 rewardsPoints,只需执行以下操作:
System.out.println(rewardsProgram.getRewardPoints());
你的class也需要理清一点,有些方法是没用的!这是完成所需操作的简单方法。
您应该解决代码中存在的以下问题:
- 构造函数里面的声明
String studentName;
,BookclubTester(){}
没用
- 您既没有为
studentName
定义 setter 和 getter 方法,也没有将其设置到对象中,rewardsProgram
。
- 在
while
循环之外实例化 Scanner
即 Scanner input = new Scanner(System.in);
应该在 while 循环之外。您可以重用在 while
循环外实例化的相同 Scanner
对象。
- 我还建议您使用
Integer.parseInt(input.nextLine())
而不是 input.nextInt()
以避免 here. 中描述的问题
- 您还没有将
rewardPoints
设置到对象 rewardsProgram
中。理想情况下,应该从 setBooks
调用 calculating/setting 奖励积分方法,因为奖励积分是基于书籍数量的。
- 书本数超过
4
时的奖励积分应该有一个条件(例如if (books >= 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
您仍然可以想到许多验证(例如检查书的数量是否不是负数),我希望这些建议能帮助您设计这些东西的程序。
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");
}
输出:
Johnny has 60 points for 5 books
你好,我正在学习 java class 并且我将这个程序作为作业。我试图找出谁来设置值和 return 使用 Scanner class 的计算。我是 java(任何语言)的初学者。如果你能帮我弄清楚这个问题。我到处都找了,但找不到解决方案。
/**
* 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++;
}
}
}
这是class
/**
*
* @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);
}
}
}
你的代码有点混乱,但对于初学者来说这是正常的:)
首先,在您的主要 class 中,您正在打印 rewardsProgram.getRewardPoints()
只是简单地 returns rewardPoints
为空!
当您在 setBooks
方法中设置书籍时,您必须调用第二个方法来计算您的 rewardsPoints,在您的例子中是 setRewardsPoints
。在这个方法中,你必须使用你的变量 books
来获得你的分数,你可以使用类似的东西:
switch(books){
case 0:
rewardsPoints=0; break;
case 1:
rewardsPoints=5; break;
...
}
这就是您计算积分的方式。要打印 rewardsPoints,只需执行以下操作:
System.out.println(rewardsProgram.getRewardPoints());
你的class也需要理清一点,有些方法是没用的!这是完成所需操作的简单方法。
您应该解决代码中存在的以下问题:
- 构造函数里面的声明
String studentName;
,BookclubTester(){}
没用 - 您既没有为
studentName
定义 setter 和 getter 方法,也没有将其设置到对象中,rewardsProgram
。 - 在
while
循环之外实例化Scanner
即Scanner input = new Scanner(System.in);
应该在 while 循环之外。您可以重用在while
循环外实例化的相同Scanner
对象。 - 我还建议您使用
Integer.parseInt(input.nextLine())
而不是input.nextInt()
以避免 here. 中描述的问题
- 您还没有将
rewardPoints
设置到对象rewardsProgram
中。理想情况下,应该从setBooks
调用 calculating/setting 奖励积分方法,因为奖励积分是基于书籍数量的。 - 书本数超过
4
时的奖励积分应该有一个条件(例如if (books >= 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
您仍然可以想到许多验证(例如检查书的数量是否不是负数),我希望这些建议能帮助您设计这些东西的程序。
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");
}
输出:
Johnny has 60 points for 5 books