尝试获取奖励积分,但无法设置“Books”的值。

huangapple go评论78阅读模式
英文:

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 &lt; 5) {
Scanner input = new Scanner(System.in);
System.out.println(&quot;Please provided your name :&quot; );
String studentName = input.nextLine();
Bookclub rewardsProgram = new Bookclub();
System.out.println(&quot;Please provide the numbers of books purchase for the  month &quot; );
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(&quot;Your points are:&quot; + 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(&quot;Your points are : &quot; + rewardPoints);
}else if(rewardPoints == 15) {
System.out.println(&quot;Your points are:&quot; + rewardPoints);
}else if(rewardPoints == 30) { 
System.out.println(&quot;Your points are:&quot; + rewardPoints);
}else if(rewardPoints == 60) { 
System.out.println(&quot;Your points are:&quot; + rewardPoints);
}  
}
}

答案1

得分: 0

你的代码中有一些混淆,但对于初学者来说这是正常的 尝试获取奖励积分,但无法设置“Books”的值。

首先,在你的主类中,你正在打印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 尝试获取奖励积分,但无法设置“Books”的值。

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:

  1. The declaration String studentName; inside the constructor, BookclubTester(){} is useless.
  2. You have neither defined the setter and getter methods for studentName nor set it into the object, rewardsProgram.
  3. Instantiate Scanner outside the while loop i.e. Scanner input = new Scanner(System.in); should be outside the while loop. You can reuse the same Scanner object instantiated outside the while loop.
  4. I also suggest you use Integer.parseInt(input.nextLine()) instead of input.nextInt() to avoid problem described here.
  5. You have not set rewardPoints into the object, rewardsProgram. Ideally, the method calculating/setting the reward points should be called from setBooks because the reward points are based on the number of books.
  6. There should be a condition (e.g. if (books &gt;= 4)) for the reward points when the number of books is more than 4.

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 &gt;= 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 &lt; 5) {
Bookclub rewardsProgram = new Bookclub();
System.out.print(&quot;Please provided your name: &quot;);
String studentName = input.nextLine();
rewardsProgram.setStudentName(studentName);
System.out.print(&quot;Please provide the numbers of books purchase for the  month: &quot;);
books = Integer.parseInt(input.nextLine());
rewardsProgram.setBooks(books);
System.out.println(&quot;Reward points: &quot; + 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 &lt;= 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 &quot;Platinum Member&quot;... 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&#39;m cheating and manually putting them in :)
BookClubMember member = new BookClubMember(&quot;Johnny&quot;, 5);
System.out.println(member.getName() + &quot; has &quot; + member.getRewardPoints() + &quot; points &quot; + &quot; for &quot; + member.getBooks() + &quot; books&quot;);
}

Output:

> Johnny has 60 points for 5 books

huangapple
  • 本文由 发表于 2020年10月18日 20:38:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/64413392.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定