2017-04-21 83 views
0

我目前正在學習Java,我有一個任務,要求我編寫for循環。我需要創建一個允許用戶輸入的小程序,然後使用for循環將消息告訴用戶他們的信息。我需要for循環來允許,總結用戶投入的天數,以及他們每天的穀物數量,同時我還需要每天獲得兩倍的穀物數量。創建一個for循環以使之前的值翻倍

實施例:

第1天你得到1粒米,總共1個晶粒

日的2你得到2個晶粒大米的總共3粒

3你日大米共計7粒

4日的4粒你得到8粒米,共15粒

X天的你米X穀物的TOT Y晶粒

我不完全確定如何設置我的for循環來做到這一點。這是我到目前爲止。

public static void GrainCounter() 
{ 
    Scanner s = new Scanner(System.in); 
    System.out.println("How many days worth of grain do you have?"); 
    int days = s.nextInt(); 
    int sum = 0; 

    for (int i = 0; i <= days; i++) 
    { 
    sum = sum + i; 
    } 
    System.out.print("Day " + days + " you got " + sum + " grains of rice"); 
} 
+0

請按照一個簡單的Java教程,它會清楚地指出它,加上一堆其他*有用的東西。 – rotgers

+0

請在這裏添加ypur代碼而不是圖片。 – Moon

+0

'sum + = 1 << i;' – shmosel

回答

0

的公式實際上是,

Day X you get 2**X grains of rice for a total of Y grains 

sum應該1開始,你應該把你print循環(也想要打印i爲天)。喜歡的東西,

System.out.println("Day 1 you get 1 grain of rice for a total of 1 grain"); 
int sum = 1; 
for (int i = 1; i <= days; i++) 
{ 
sum += Math.pow(2, i); 
System.out.println("Day " + (i + 1) + " you got " + (int)Math.pow(2,i) + 
    " grains of rice for a total of " + sum + " grains"); 
} 

你也可以寫println作爲

System.out.printf("Day %d you got %d grains of rice for a total of %d grains%n", 
     i + 1, (int) Math.pow(2, i), sum); 
+1

我不知道爲什麼接受你的答案,即使它給出了錯誤的結果!你運行它了嗎?! – Yahya

+0

@John我確實在'sum'行位置有一個bug(我已經修復),爲什麼你接受我的回答? –

+0

我是如何接受你的答案的? – Yahya

0

所有你需要做的是初始化rice1,只是不斷增加它本身在for -loop,直到你的循環迭代int i達到天數-1(因爲您初始化爲1,所以不需要額外的迭代)

public static int doubleDays(int days) { 
     int rice = 1; 
     for (int i = 0; i < days - 1; i++) { 
      rice += rice; 
     } 
     return rice; 
    } 
0

試試這個:

import java.util.Scanner; 
public class GrainCounter{ 
    public static void main(String[] agrs){ 
     Scanner in = new Scanner(System.in); 
     System.out.println("How Many Days Worth Of Grain Do You Have?"); 
     int days = in.nextInt(); 
     int sum =1, dailySum=1; 
     System.out.println("Day 1 you get 1 grain of rice for a total of 1 grain"); 
     for (int i =1; i <days; i++){ 
      dailySum*=2; 
      sum +=dailySum; 
      System.out.print("Day "+ (i+1) + " you get " + dailySum 
               + " grain of rice for a total of " 
               + sum + " grain\n"); 
    } 
    } 
} 

結果將是10天:

How Many Days Worth Of Grain Do You Have? 
10 
Day 1 you get 1 grain of rice for a total of 1 grain 
Day 2 you get 2 grain of rice for a total of 3 grain 
Day 3 you get 4 grain of rice for a total of 7 grain 
Day 4 you get 8 grain of rice for a total of 15 grain 
Day 5 you get 16 grain of rice for a total of 31 grain 
Day 6 you get 32 grain of rice for a total of 63 grain 
Day 7 you get 64 grain of rice for a total of 127 grain 
Day 8 you get 128 grain of rice for a total of 255 grain 
Day 9 you get 256 grain of rice for a total of 511 grain 
Day 10 you get 512 grain of rice for a total of 1023 grain 
0

問題是你的代碼試圖使用一天作爲變量,但實際上您的變量實際上是前幾天的獲得。

public static void GrainCounter(){ 
    Scanner s = new Scanner(System.in); 
    System.out.println("How many days worth of grain do you have?"); 
    int days = s.nextInt(); 
    int sum = 1; 
    previousDay = 1; 
    for (int i = 2; i <= days; i++){ 
     sum += previousDay*2; 
     previousDay = previousDay*2; 
    } 
    System.out.print("Day " + days + " you got " + sum + " grains of rice"); 
}