英文:
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 <= 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);
}
答案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 <= b; i++) {
if (i % 3 == 0) {
System.out.println(i);
sum += i;
count++;
}
}
System.out.println("Average: " + 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 <= b; i++) {
if (i % 3 == 0) {
System.out.println(i);
sum += i;
count++;
}
}
System.out.println("Average: " + sum / count);
}
}
A sample run:
2
10
3.0
6.0
9.0
Average: 6.0
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论