如何在 java 中存储整数的更新值?
How to store updated value of an integer in java?
这是我的自动售货机代码-
import java.util.Scanner;
public class VendingMachine
{
public static final Scanner sc = new Scanner(System.in);
public static double totalAmt;
public static double change;
public static String food = null;
public static int stockPC = 2;
public static int stockCo = 2;
public static int stockCa = 2;
public static void MainMenu(){
System.out.println("Main menu - ");
System.out.println("a - Potato Chips -.25");
System.out.println("b - Cookies - [=11=].85 ");
System.out.println("c - Candies - [=11=].95");
String input = sc.next().toLowerCase();
change = 0;
if(input.equals("a")){
stockPC = stockPC -1;
change = totalAmt - 1.25;
food ="Potato chips";
if(stockPC == 0){
System.out.println("Sorry we're out of Potato Chips:(");
}else{
System.out.println("Please take your "+ food + "\n");
System.out.println("Here is your change $" + change);
}
}else if ( input.equals("b")){
stockCo = stockCo -1;
change = totalAmt - 0.85;
food ="Cookies";
if(stockCo == 0){
System.out.println("Sorry we're out of Cookies:(");
}else{
System.out.println("Please take your "+ food + "\n");
System.out.println("Here is your change $" + change);
}
}else if ( input.equals("c")){
stockCa = stockCa -1;
change = totalAmt - 0.95;
food ="Candies";
if(stockCa == 0){
System.out.println("Sorry we're out of Candies:(");
}else{
System.out.println("Please take your "+ food + "\n");
System.out.println("Here is your change $" + change);
}
}else{
System.out.println("Our system only accepts a,b or c");
}
}
public static void main(String[] args){
System.out.println("Hi! Welcome to Vending Machine!");
System.out.println("How many quarters do you have?");
double noOfQt = sc.nextDouble();
totalAmt = noOfQt * 0.25;
System.out.println("How many dimes do you have?");
double noOfDm = sc.nextDouble();
totalAmt = totalAmt + (noOfDm * 0.1);
System.out.println("How many nickels do you have?");
double noOfNk = sc.nextDouble();
totalAmt = totalAmt + (noOfNk * 0.05);
System.out.println("You have = $" + totalAmt);
MainMenu();
}
}
我的 MainMenu 方法有问题。在我的 MainMenu 方法中选择一个输入后,我想在方法结束时更新 'integers' 的值,如 change、stockPC、stockCo 和 stockCo。我不想打印这些值。我只想在我的主要方法中使用这些更新后的值。
我该怎么做?
你在哪里实例化你的扫描仪?它需要成为 main 方法的一部分,然后将来自用户的值传递给 MainMenu(-put value here-) 方法。这样 -> 扫描仪就可以工作了。
MainMenu(double amount);
在接收方法中进行计算,只传递来自用户的“通用”双重输入
此外,在您的情况下,将库存中的商品声明为 public 静态并不是一个好的做法。请记住,将变量声明为私有变量并使用 setters/getters 始终是一个好习惯。
不应像您那样初始化数据成员,而是:
private int stockPC;
private int stockCo;
private int stockCa;
使用构造函数定义初始化时的值:
public VendingMachine(int stockPC, int stockCo, int stock){
this.stockPC = stockPC;
this.stockCo = stockCo;
this.stockCa = stockCa;
}
最后 getters/setters:
public int getStockPC(){
return stockPC;}
public int getstockCo(){
return stockCo;}
public int stockCa(){
return stockCa;}
public void setstockPC(int stockPC){
this.stockPC = stockPC;}
public void stockCo(int stockCo){
this.stockCo = stockCo;}
public void stockCa(int stockCa){
this.stockCa = stockCa;}
希望对您有所帮助
我做了一台永不停止服务的自动售货机。 :D
如果您的金额少于 select 选项的价格,我也添加了金额条件,它将显示消息。
您的代码是正确的,因为您需要的只是缺少一些条件并且不正确。你想要的是 while 循环。但我已根据我的理解更正了您的条件并添加了所需的条件。如果你愿意,你可以删除这些条件。
import java.util.Scanner;
public class Main
{
public static final Scanner sc = new Scanner(System.in);
public static double totalAmt;
public static double change;
public static String food = null;
public static int stockPC = 2;
public static int stockCo = 2;
public static int stockCa = 2;
public static void MainMenu(){
System.out.println("Main menu - ");
System.out.println("a - Potato Chips -.25");
System.out.println("b - Cookies - [=10=].85 ");
System.out.println("c - Candies - [=10=].95");
String input = sc.next().toLowerCase();
change = 0;
if(input.equals("a")){
stockPC = stockPC -1;
//when stockPC is -1 means stock was zero and you want 1 more
if(stockPC == -1){
System.out.println("Sorry we're out of Potato Chips:(");
}else{
change = totalAmt - 1.25;
if(change<0){
System.out.println("You don't have enough amount to buy");
}else{
food ="Potato chips";
System.out.println("Please take your "+ food + "\n");
System.out.println("Here is your change $" + change);
}
}
}else if ( input.equals("b")){
stockCo = stockCo -1;
//when stockCo is -1 means stock was zero and you want 1 more
if(stockCo == -1){
System.out.println("Sorry we're out of Cookies:(");
}else{
change = totalAmt - 0.85;
if(change<0){
System.out.println("You don't have enough amount to buy");
}else{
food ="Cookies";
System.out.println("Please take your "+ food + "\n");
System.out.println("Here is your change $" + change);
}
}
}else if ( input.equals("c")){
stockCa = stockCa -1;
//when stockCa is -1 means stock was zero and you want 1 more
if(stockCa == 0){
System.out.println("Sorry we're out of Candies:(");
}else{
change = totalAmt - 0.95;
if(change<0){
System.out.println("You don't have enough amount to buy");
}else{
food ="Candies";
System.out.println("Please take your "+ food + "\n");
System.out.println("Here is your change $" + change);
}
}
}else{
System.out.println("Our system only accepts a,b or c");
}
}
public static void main(String[] args){
System.out.println("Hi! Welcome to Vending Machine!");
System.out.println("How many quarters do you have?");
double noOfQt = sc.nextDouble();
totalAmt = noOfQt * 0.25;
System.out.println("How many dimes do you have?");
double noOfDm = sc.nextDouble();
totalAmt = totalAmt + (noOfDm * 0.1);
System.out.println("How many nickels do you have?");
double noOfNk = sc.nextDouble();
totalAmt = totalAmt + (noOfNk * 0.05);
System.out.println("You have = $" + totalAmt);
while(true){
MainMenu();
}
}
}
这是我的自动售货机代码-
import java.util.Scanner;
public class VendingMachine
{
public static final Scanner sc = new Scanner(System.in);
public static double totalAmt;
public static double change;
public static String food = null;
public static int stockPC = 2;
public static int stockCo = 2;
public static int stockCa = 2;
public static void MainMenu(){
System.out.println("Main menu - ");
System.out.println("a - Potato Chips -.25");
System.out.println("b - Cookies - [=11=].85 ");
System.out.println("c - Candies - [=11=].95");
String input = sc.next().toLowerCase();
change = 0;
if(input.equals("a")){
stockPC = stockPC -1;
change = totalAmt - 1.25;
food ="Potato chips";
if(stockPC == 0){
System.out.println("Sorry we're out of Potato Chips:(");
}else{
System.out.println("Please take your "+ food + "\n");
System.out.println("Here is your change $" + change);
}
}else if ( input.equals("b")){
stockCo = stockCo -1;
change = totalAmt - 0.85;
food ="Cookies";
if(stockCo == 0){
System.out.println("Sorry we're out of Cookies:(");
}else{
System.out.println("Please take your "+ food + "\n");
System.out.println("Here is your change $" + change);
}
}else if ( input.equals("c")){
stockCa = stockCa -1;
change = totalAmt - 0.95;
food ="Candies";
if(stockCa == 0){
System.out.println("Sorry we're out of Candies:(");
}else{
System.out.println("Please take your "+ food + "\n");
System.out.println("Here is your change $" + change);
}
}else{
System.out.println("Our system only accepts a,b or c");
}
}
public static void main(String[] args){
System.out.println("Hi! Welcome to Vending Machine!");
System.out.println("How many quarters do you have?");
double noOfQt = sc.nextDouble();
totalAmt = noOfQt * 0.25;
System.out.println("How many dimes do you have?");
double noOfDm = sc.nextDouble();
totalAmt = totalAmt + (noOfDm * 0.1);
System.out.println("How many nickels do you have?");
double noOfNk = sc.nextDouble();
totalAmt = totalAmt + (noOfNk * 0.05);
System.out.println("You have = $" + totalAmt);
MainMenu();
}
}
我的 MainMenu 方法有问题。在我的 MainMenu 方法中选择一个输入后,我想在方法结束时更新 'integers' 的值,如 change、stockPC、stockCo 和 stockCo。我不想打印这些值。我只想在我的主要方法中使用这些更新后的值。
我该怎么做?
你在哪里实例化你的扫描仪?它需要成为 main 方法的一部分,然后将来自用户的值传递给 MainMenu(-put value here-) 方法。这样 -> 扫描仪就可以工作了。
MainMenu(double amount);
在接收方法中进行计算,只传递来自用户的“通用”双重输入
此外,在您的情况下,将库存中的商品声明为 public 静态并不是一个好的做法。请记住,将变量声明为私有变量并使用 setters/getters 始终是一个好习惯。
不应像您那样初始化数据成员,而是:
private int stockPC;
private int stockCo;
private int stockCa;
使用构造函数定义初始化时的值:
public VendingMachine(int stockPC, int stockCo, int stock){
this.stockPC = stockPC;
this.stockCo = stockCo;
this.stockCa = stockCa;
}
最后 getters/setters:
public int getStockPC(){
return stockPC;}
public int getstockCo(){
return stockCo;}
public int stockCa(){
return stockCa;}
public void setstockPC(int stockPC){
this.stockPC = stockPC;}
public void stockCo(int stockCo){
this.stockCo = stockCo;}
public void stockCa(int stockCa){
this.stockCa = stockCa;}
希望对您有所帮助
我做了一台永不停止服务的自动售货机。 :D 如果您的金额少于 select 选项的价格,我也添加了金额条件,它将显示消息。
您的代码是正确的,因为您需要的只是缺少一些条件并且不正确。你想要的是 while 循环。但我已根据我的理解更正了您的条件并添加了所需的条件。如果你愿意,你可以删除这些条件。
import java.util.Scanner;
public class Main
{
public static final Scanner sc = new Scanner(System.in);
public static double totalAmt;
public static double change;
public static String food = null;
public static int stockPC = 2;
public static int stockCo = 2;
public static int stockCa = 2;
public static void MainMenu(){
System.out.println("Main menu - ");
System.out.println("a - Potato Chips -.25");
System.out.println("b - Cookies - [=10=].85 ");
System.out.println("c - Candies - [=10=].95");
String input = sc.next().toLowerCase();
change = 0;
if(input.equals("a")){
stockPC = stockPC -1;
//when stockPC is -1 means stock was zero and you want 1 more
if(stockPC == -1){
System.out.println("Sorry we're out of Potato Chips:(");
}else{
change = totalAmt - 1.25;
if(change<0){
System.out.println("You don't have enough amount to buy");
}else{
food ="Potato chips";
System.out.println("Please take your "+ food + "\n");
System.out.println("Here is your change $" + change);
}
}
}else if ( input.equals("b")){
stockCo = stockCo -1;
//when stockCo is -1 means stock was zero and you want 1 more
if(stockCo == -1){
System.out.println("Sorry we're out of Cookies:(");
}else{
change = totalAmt - 0.85;
if(change<0){
System.out.println("You don't have enough amount to buy");
}else{
food ="Cookies";
System.out.println("Please take your "+ food + "\n");
System.out.println("Here is your change $" + change);
}
}
}else if ( input.equals("c")){
stockCa = stockCa -1;
//when stockCa is -1 means stock was zero and you want 1 more
if(stockCa == 0){
System.out.println("Sorry we're out of Candies:(");
}else{
change = totalAmt - 0.95;
if(change<0){
System.out.println("You don't have enough amount to buy");
}else{
food ="Candies";
System.out.println("Please take your "+ food + "\n");
System.out.println("Here is your change $" + change);
}
}
}else{
System.out.println("Our system only accepts a,b or c");
}
}
public static void main(String[] args){
System.out.println("Hi! Welcome to Vending Machine!");
System.out.println("How many quarters do you have?");
double noOfQt = sc.nextDouble();
totalAmt = noOfQt * 0.25;
System.out.println("How many dimes do you have?");
double noOfDm = sc.nextDouble();
totalAmt = totalAmt + (noOfDm * 0.1);
System.out.println("How many nickels do you have?");
double noOfNk = sc.nextDouble();
totalAmt = totalAmt + (noOfNk * 0.05);
System.out.println("You have = $" + totalAmt);
while(true){
MainMenu();
}
}
}