英文:
trying to get the reward points and I can't set the value of Books
问题
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 provide your name:");
String studentName = input.nextLine();
Bookclub rewardsProgram = new Bookclub();
System.out.println("Please provide the number of books purchased for the month:");
books = input.nextInt();
rewardsProgram.setBooks(books);
System.out.println(rewardsProgram.getRewardPoints());
count++;
}
}
}
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);
}
}
}
Note: I've translated the code part as per your request, making sure only the code is provided without any additional explanations.
英文:
Hello I am taking java class and I have this program as an assignment. I am trying to figure out who to set the value and return the calculation using Scanner class. I am a total beginner in java ( in any comp. language). if you can help me figure out the issue. I have look everywhere but I can't find the solution.
/**
* 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++;
}
}
}
This is the 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);
}
}
}
答案1
得分: 0
你的代码中有一些混淆,但对于初学者来说这是正常的
首先,在你的主类中,你正在打印rewardsProgram.getRewardPoints()
,它只是简单地返回了rewardPoints
,而该变量是空的!
当你在setBooks
方法中设置你的书籍时,你必须调用第二个方法来计算你的奖励积分,在你的情况下,这就是setRewardsPoints
方法。在这个方法中,你必须使用你的变量books
来获取你的积分,你可以使用类似这样的方式:
switch(books){
case 0:
rewardsPoints=0; break;
case 1:
rewardsPoints=5; break;
...
}
这是你应该计算积分的方法。要打印积分,只需执行:
System.out.println(rewardsProgram.getRewardPoints());
你还需要稍微清理一下你的类,有些方法是多余的!这是一个实现你所需功能的简单方法。
英文:
There is a little bit of confusion in your code, but for beginners it's normal
First of all, in your main class you are printing rewardsProgram.getRewardPoints()
that just simply returns rewardPoints
which is null!
When you set your books inside setBooks
method you have to call a second method to calculate your rewardsPoints, in your case it's the setRewardsPoints
. Inside this method you have to use your variable books
to get your points, you can use something like:
switch(books){
case 0:
rewardsPoints=0; break;
case 1:
rewardsPoints=5; break;
...
}
This is how you should calculate your points. To print rewardsPoints, simply do:
System.out.println(rewardsProgram.getRewardPoints());
You also need to clear a little bit your class, some methods are useless! This is a simple way to do what you need.
答案2
得分: 0
以下是代码的翻译部分:
你应该解决代码中存在的以下问题:
1. 在构造函数 `BookclubTester(){}` 内部的声明 `String studentName;` 是多余的。
2. 你既没有为 `studentName` 定义 setter 和 getter 方法,也没有将其设置到对象 `rewardsProgram` 中。
3. 将 `Scanner` 实例化放在 `while` 循环之外,即 `Scanner input = new Scanner(System.in);` 应该在 while 循环之外。你可以重复使用在 while 循环之外实例化的同一个 `Scanner` 对象。
4. 我还建议你使用 `Integer.parseInt(input.nextLine())` 替代 `input.nextInt()`,以避免 [这里描述的问题](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo)。
5. 你没有将 `rewardPoints` 设置到对象 `rewardsProgram` 中。理论上,计算/设置奖励点数的方法应该从 `setBooks` 调用,因为奖励点数是基于书的数量的。
6. 当书的数量超过 `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("请输入您的姓名:");
String studentName = input.nextLine();
rewardsProgram.setStudentName(studentName);
System.out.print("请输入本月购买的书籍数量:");
books = Integer.parseInt(input.nextLine());
rewardsProgram.setBooks(books);
System.out.println("奖励点数:" + rewardsProgram.getRewardPoints());
count++;
}
}
}
**一个示例运行:**
请输入您的姓名:Test1
请输入本月购买的书籍数量:2
奖励点数:15
请输入您的姓名:Test2
请输入本月购买的书籍数量:1
奖励点数:5
请输入您的姓名:Test3
请输入本月购买的书籍数量:7
奖励点数:60
请输入您的姓名:Test4
请输入本月购买的书籍数量:0
奖励点数:0
请输入您的姓名:Test5
请输入本月购买的书籍数量:4
奖励点数:60
你仍然可以考虑许多验证(例如,检查书的数量是否为负数),我希望这些建议能帮助你为这些问题设计程序。
英文:
You should address the following problems present in your code:
- The declaration
String studentName;
inside the constructor,BookclubTester(){}
is useless. - You have neither defined the setter and getter methods for
studentName
nor set it into the object,rewardsProgram
. - Instantiate
Scanner
outside thewhile
loop i.e.Scanner input = new Scanner(System.in);
should be outside the while loop. You can reuse the sameScanner
object instantiated outside thewhile
loop. - I also suggest you use
Integer.parseInt(input.nextLine())
instead ofinput.nextInt()
to avoid problem described here. - You have not set
rewardPoints
into the object,rewardsProgram
. Ideally, the method calculating/setting the reward points should be called fromsetBooks
because the reward points are based on the number of books. - There should be a condition (e.g.
if (books >= 4)
) for the reward points when the number of books is more than4
.
Given below is the code incorporating these recommendations:
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++;
}
}
}
A sample run:
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
You can still think of many validations (e.g. check if the number of books is not negative) and I hope, these suggestions will help you design your program for those things.
答案3
得分: 0
Sure, here's the translated code:
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;
}
}
}
And your main method:
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");
}
Output:
> Johnny has 60 points for 5 books
英文:
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;
}
}
}
And your main method
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");
}
Output:
> Johnny has 60 points for 5 books
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论