[英]Saving java variables
由於這個問題,我正在做作業,無法執行功能。
在程序的開始,我創建了一個變量
int initial = Keyboard.nextInt() (obviously user input)
然后程序進入循環,其中“ initial”的值被多次更改。 但是,在程序結束時,當我們退出循環時,我需要使用新的“初始”值以及用戶最初輸入的確切值。
我很難讓程序找到初始值,因為在退出循環后,每次嘗試調用“ initial”變量時,我只會得到更改后的數字,而不是用戶輸入的第一個數字。 謝謝您關於如何解決此問題的幫助。
public class Question3 {
public static void main(String[] args) {
//declare scanner
Scanner keyboard = new Scanner (System.in);
//initial amount
System.out.println("Enter the initial amount: ");
int initial = keyboard.nextInt();
int numItems = 0;
double assets = 0;
int originalPrice=initial;
double spending = originalPrice-assets;
if(initial<=10)
{
System.out.println("Please save money and come back later!!");
numItems=1;
spending=0.0;
}
else
while (initial > 10 )
{
System.out.println("Do you want to make purchases (Y/N)? ");
char c = keyboard.next().charAt(0);
if (c == 'Y')
{
System.out.println("Please enter the price of the item = ");
}
else
{
System.out.println("Lack of desire of Mr.Toto");
break;
}
int price = keyboard.nextInt();
if (initial-price>=10)
{
System.out.println("A purchase is accepted");
initial-=price;
assets=initial-price;
numItems++;
}
else
{
System.out.println("Insufficient assets!!");
System.out.println("Please enter the price of the item = ");
}
if(numItems==10)
{
System.out.println("Maximal number of purchases reached");
break;
}
}
//displaying the summary of mr totos purchases
System.out.println("-------------------------------------------");
System.out.println("Here is a summary of Mr.Toto's purchases.");
System.out.println("-------------------------------------------");
System.out.println("Number of items Assets Spending");
System.out.println(" "+numItems+" "+assets+" " +" "+spending);
}
}
我認為您應該嘗試使用ArrayList。 例如:
//Inside loop
ArrayList<Integer> stuff = new ArrayList<>();
price = Keyboard.nextInt();
stuff.add(price);
//Outside loop; gets first input
stuff.get(0);
有多個錯誤。
int
值,因此刪除了doubles
。 initialBalance
應該是不可變的,因此將其設置為final
。 spending
變量應已初始化為0
。 package com.stackoverflow.q42588622;
import java.util.Scanner;
@SuppressWarnings("javadoc")
public class Answer {
public static void main(String[] args) {
// get in the habit of cleaning up resources
try (Scanner keyboard = new Scanner(System.in)) {
// initial amount
System.out.println("Enter the initial amount: ");
final int initialBalance = keyboard.nextInt();
int numItems = 0;
// Same concept as balance
// Only dealing with ints
// double assets = 0.0;
int balance = initialBalance;
// Spending is initially 0.
// Only dealing with ints
// double spending = originalPrice - assets;
int spending = 0;
if (initialBalance <= 10) {
System.out.println("Please save money and come back later!!");
numItems = 1;
spending = 0;
} else {
while (balance > 10) {
System.out.println("Do you want to make purchases (Y/N)? ");
char c = keyboard.next()
.charAt(0);
if (c == 'Y' || c == 'y') {
System.out.println("Please enter the price of the item = ");
}
else {
System.out.println("Lack of desire of Mr.Toto");
break;
}
int price = keyboard.nextInt();
if (balance - price >= 10) {
System.out.println("A purchase is accepted");
balance -= price;
spending += price;
// Removing as initialBalance is immutable
// assets = initial - price;
numItems++;
}
else {
System.out.println("Insufficient assets!!");
System.out.println("Please enter the price of the item = ");
}
if (numItems == 10) {
System.out.println("Maximal number of purchases reached");
break;
}
}
}
// displaying the summary of mr totos purchases
System.out.println("-------------------------------------------");
System.out.println("Here is a summary of Mr.Toto's purchases.");
System.out.println("-------------------------------------------");
System.out.println("Number of items Assets Spending");
System.out.println(" " + numItems + " " + balance + " " + " " + spending);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.