私は割り当てを行っており、この問題のために関数を実行できません。私はプログラムをし、「初期」の値が何度も変更されているループに入り、可変Java変数を保存する
int initial = Keyboard.nextInt() (obviously user input)
を作成したプログラムの先頭に
。しかし、プログラムの終わりにループを終了するときには、新しい「初期値」とユーザーが最初に入力した正確な値を使用する必要があります。
私は最初の変数を呼び出そうとするたびにループを終了してから、ユーザーが入力した最初の変数ではなく変更された番号しか取得できないため、プログラムに初期値を見つけるのが難しいです。この問題をどのように解決できるかについてのヘルプは、ありがとうと思います。
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);
}
}
ただ、こんにちは新しい入力用に別の変数と古い入力 – UnknowableIneffable
を使用StackOverflowのために歓迎!あなたは、ガイドラインを参照する必要がありますhttp://stackoverflow.com/help/how-to-askコード例などを提供する必要があります あなたの質問には、あなたが言っているように、int price = nextInt()を得るだけです。別の変数originalPrice = price。新しい価格のためのコール価格とoldのためのoriginalPrice。 –
助けてくれてありがとう – peanut