创建for循环以将之前的值加倍

时间:2017-04-21 23:05:52

标签: java for-loop parameters expression bluej

我目前正在学习Java,我有一个任务要求我写一个for循环。我需要创建一个允许用户输入的小程序,然后使用for循环来发送消息告诉用户他们的信息。我需要for循环允许,总结用户放置的天数和每天他们的颗粒,我还需要每天获得两倍的谷物量。

实施例

第1天你得到1粒大米,总共1粒

第2天你得到2粒大米,总共3粒

第3天你得到4粒大米,总共7粒

第4天你得到8粒大米,总共15粒

第X天你得到X粒米粒,共计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");
}

4 个答案:

答案 0 :(得分:0)

公式实际上是

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); 写为

let highscore = 1000  //this is the value
//below is the saving part
let userDefaults = NSUserDefaults.standardUserDefaults()
userDefaults.setValue(highscore, forKey: "highscore")
userDefaults.synchronize()

答案 1 :(得分:0)

您需要做的就是使用rice初始化1并继续在for - 循环中将其添加到自身,直到您的循环迭代器int i达到days - 1(因为你初始化为1,因此没有额外的迭代)

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

答案 2 :(得分: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

答案 3 :(得分: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");
}