当我运行这段代码并输入数字时
public static void main(String[] args) {
//Use Main Method for gathering input
float input = 1;
// Declare variable for sum
float theSum = 0;
// Declare variable for average
float average = 0;
// Declare variable for counting the number of user inputs
int counter = 0;
/* Initialize the while loop using an input of 0 as a sentinel value
* to exit the loop*/
while (input != 0) {
// Use JOptionPane method to accept input from user
input = Float.parseFloat(
JOptionPane.showInputDialog(
null, "Please enter a number. Enter 0 to quit: "));
// Invoke sum method and pass input and summation to sum method
theSum = (sum(input, theSum));
// Invoke avg method and pass summation and counter to avg
average = (avg(theSum, counter));
// Increment the counter variable
counter++;
if (theSum > 100)
{
JOptionPane.showMessageDialog(null, "The sum of your numbers "
+ "are greater than 100!");
break;
}
}
// Invoke display method and pass summation, average, and counter variables to it
display(theSum, average, counter);
}
public static float sum(float num1, float sum) {
//Add the user's input number to the sum variable
sum += num1;
//Return value of sum variable as new summation variable
return sum;
}
public static float avg(float num1, int num2) {
//Declare and initialize variable for average
float average = 0;
//Calculate average
average = num1 / num2;
//Return value of average variable
return average;
}
public static void display(float sum, float average, int counter) {
/* I am subtracting 1 from variable counter so as not to include the sentinel value
* of 0 that the user had to enter to exit the input loop in the overall count*/
// Display the count, sum, and average to the user
if (sum > 100) {
JOptionPane.showMessageDialog(null, "Count = " + (counter) + ", Sum = " + sum + ", Average = " + average);
}
if (sum <= 100) {
JOptionPane.showMessageDialog(null, "Count = " + (counter - 1) + ", Sum = " + sum + ", Average = " + average);
}
}
}
在取平均值后,您将计数器递增,这就是为什么您看到平均值基于2个数字,而不是预期的3个数字。
average = (avg(theSum, counter));
// Increment the counter variable
counter++;
在取平均值之前,交换这两个并递增计数器。
counter++;
// Increment the counter variable
average = (avg(theSum, counter));
编辑:
以下是您应该更改的内容:
首先,仅在输入不=0时更新计数器
if(input!=0)
{
counter++;
}
其次,将平均代码移出循环,并在显示之前将其放在最后,不需要一次又一次地计算平均值。
average = (avg(theSum, counter));
display(theSum, average, counter);
第三,从显示
方法和打印计数器中删除计数器1
public static void display(float sum, float average, int counter) {
JOptionPane.showMessageDialog(null, "Count = " + (counter) + ", Sum = " + sum + ", Average = " + average);
}
之后,这两种情况下都会像您预期的那样起作用
null
当我运行代码时,它说: 回溯(最近一次调用):文件“C:\Users\Gebruiker\PycharmProjects\pythonProject\DiscordBot.py”,第1行,导入discord模块NotFoundError:没有名为“discord”的模块 idk如何解决这个问题,请帮助我。
我正在使用多个页面,每个页面都有jQuery选项卡。假设Page1.html带有#tab1和#tab2,Page2.html带有#tab3和#tab4。我的代码存在以下问题: 1)在标签内容中,Page1.html#tab2有一个指向Page1.html#tab1.的超链接链接不起作用——当单击链接时,页面只是停留在#tab1上。但是,Page1上的菜单容器中的一个到#tab1的超链接确实有效。两
我不太理解while循环中的条件,以及它代表什么'>>>='。
其目的是对用户输入的分数进行排序、显示和平均。我还没弄明白。如果你已经解决了这个问题,请帮忙。到目前为止,我已经尝试了这段代码,但它不起作用。 我从这段代码中得到的只是一个编译器错误。有人有什么建议吗?谢谢你。
我有这个错误:值错误:检查输入时的错误:预期dense_1_input有形状(6,),但得到了形状(1,)数组,但我的keras模型的输入层model.add(密集(单位=5,kernel_initializer='统一',激活='relu',input_dim=6))所以(6,)维度和输入是输入=np.array([HeadX, HeadY, TailX, TailY, AppleX, Appl