gpt4 book ai didi

java - 非静态/静态变量错误

转载 作者:行者123 更新时间:2023-11-29 08:14:35 25 4
gpt4 key购买 nike

我正在制作一个程序,可以获取裁判分数并给出平均值,我认为我做错了一个变量。我收到错误消息:

non-static variable difficulty cannot be referenced from a static context
difficulty = keyboard.nextDouble;

我已经尝试将它移到 main 方法之上并向它添加“static”,但它似乎仍然给我带来问题。还有什么我可以做的吗?这是它的部分代码

import java.util.Scanner;
public class CH6PA
{
private double difficulty;
private int score;
private double[] average;

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

do
{
System.out.println("Enter the level of difficulty (1.2-3.8)");
difficulty = keyboard.nextDouble;
}
while (difficulty>1.2 || difficulty<3.8);


int judge = new int[7];


for(int i = 0; i<judge.length; i++)
{

最佳答案

其他答案是关于将事物更改为静态的,您应该避免在静态变量中具有可变状态这是编程的 101 - 它本质上是全局变量。

import java.util.Scanner;
public class CH6PA
{
private double difficulty;
private int score;
private double[] average;

public static void main(String[]args)
{
CH6PA ch6pa = new CH6PA();
ch6pa.doSomething();
}

private void doSomething() {
Scanner keyboard = new Scanner (System.in);

do {
System.out.println("Enter the level of difficulty (1.2-3.8)");
difficulty = keyboard.nextDouble;
} while (difficulty>1.2 || difficulty<3.8);

int judge = new int[7];

for(int i = 0; i<judge.length; i++) {
}

关于java - 非静态/静态变量错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5590949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com