制作一个根据用户要求打印从 x 到 y 的数字的程序。

huangapple go评论78阅读模式
英文:

Making a program that prints from x to y numbers depending on what the user asks

问题

I'm new to Java sorry, I have to make this program in which it will ask the user two numbers, from those two numbers take the intervals that can be divided by 3 and then make an average of them.

I'm only focusing on making it print all the numbers since I'm trying to go step by step.

I have tried two things

On this first code it asks for both numbers but doesn't print anything

Scanner scanner = new Scanner (System.in);

double b = scanner.nextDouble();

for ( double a = scanner.nextDouble() ; a <= b; a++)
{
    System.out.println(a);
}

On this second code it asks for 3 inputs and it prints the loop normally if the last input is equal to the first, I think I can see why this is happening but I don't know how to fix it:

Scanner scanner = new Scanner (System.in);

double a = scanner.nextDouble();
double b = scanner.nextDouble();

for ( a = scanner.nextDouble() ; a <= b; a++)
{
    System.out.println(a);
}
英文:

I'm new to Java sorry, I have to make this program in which it will ask the user two numbers, from those two numbers take the intervals that can be divided by 3 and then make an average of them.

I'm only focusing on making it print all the numbers since I'm trying to go step by step.

I have tried two things

On this first code it asks for both numbers but doesn't print anything

Scanner scanner = new Scanner (System.in);

double b = scanner.nextDouble();
  
for ( double a = scanner.nextDouble() ; a &lt;= b; a++)
{
    System.out.println(a);
}

On this second code it asks for 3 inputs and it prints the loop normally if the last input is equal to the first, I think I can see why this is happening but I don't know how to fix it:

    Scanner scanner = new Scanner (System.in);
    
    double a = scanner.nextDouble();
    double b = scanner.nextDouble();
  
    for ( a = scanner.nextDouble() ; a &lt;= b; a++)
    {
        System.out.println(a);
    }

答案1

得分: 0

你需要使用变量 a 的值来初始化循环计数器,并使循环迭代直到变量 b 的值,如下所示:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int a = scanner.nextInt();
        int b = scanner.nextInt();
        int count = 0;
        double sum = 0;
        for (int i = a; i <= b; i++) {
            if (i % 3 == 0) {
                System.out.println(i);
                sum += i;
                count++;
            }
        }
        System.out.println("Average: " + sum / count);
    }
}

一个示例运行:

2
10
3
6
9
平均值:6.0

-或-

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        double a = scanner.nextDouble();
        double b = scanner.nextDouble();
        int count = 0;
        double sum = 0;
        for (double i = a; i <= b; i++) {
            if (i % 3 == 0) {
                System.out.println(i);
                sum += i;
                count++;
            }
        }
        System.out.println("平均值:" + sum / count);
    }
}

一个示例运行:

2
10
3.0
6.0
9.0
平均值:6.0
英文:

You need to initialize the loop counter with the value of a and make the loop iterate until the value of b as shown below:

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);

		int a = scanner.nextInt();
		int b = scanner.nextInt();
		int count = 0;
		double sum = 0;
		for (int i = a; i &lt;= b; i++) {
			if (i % 3 == 0) {
				System.out.println(i);
				sum += i;
				count++;
			}
		}
		System.out.println(&quot;Average: &quot; + sum / count);
	}
}

A sample run:

2
10
3
6
9
Average: 6.0

-OR-

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);

		double a = scanner.nextDouble();
		double b = scanner.nextDouble();
		int count = 0;
		double sum = 0;
		for (double i = a; i &lt;= b; i++) {
			if (i % 3 == 0) {
				System.out.println(i);
				sum += i;
				count++;
			}
		}
		System.out.println(&quot;Average: &quot; + sum / count);
	}
}

A sample run:

2
10
3.0
6.0
9.0
Average: 6.0

huangapple
  • 本文由 发表于 2020年8月2日 07:20:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63210944.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定