Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java while和for循环有问题,使用N作为次数_Java - Fatal编程技术网

Java while和for循环有问题,使用N作为次数

Java while和for循环有问题,使用N作为次数,java,Java,我无法计算while或for循环?我还需要能够使用n=6并将其声明为最终int 因此,如果这是我当前的代码,我应该怎么做?我很抱歉,任何帮助都是巨大的帮助 // Programmer: Eric Gen S01234567 // File: Week3.java // Date: April 24 2020 // Purpose: COIT11222 assignment one question one T120 // Use println method to print initials

我无法计算while或for循环?我还需要能够使用n=6并将其声明为最终int

因此,如果这是我当前的代码,我应该怎么做?我很抱歉,任何帮助都是巨大的帮助

// Programmer: Eric Gen S01234567
// File: Week3.java
// Date: April 24 2020
// Purpose: COIT11222 assignment one question one T120
// Use println method to print initials using asterisks

import java.util.Scanner;
public class Week4
{
public static void main (String [] args)
    {
        int NumberOfTopings = 0;                                                                            //Declaring Number of Toppings

        final double SmallToping = 0.75;                                                                    //Declaring Base charge of pizza
        final double MediumToping = 1.25;
        final double LargeToping = 1.75;                                                                    //Declaring Charge Per Topping in decimal
        double Charge = 0;                                                                                  //Declaring Charge of item
        final double Small = 6.50;
        final double Medium = 10.50;
        final double Large = 14.50;                                                                         //Declaring Base charge of pizza
        final double n = 6;

        Scanner inNumber= new Scanner (System.in);                                                          //Crate Scanner for input of number of toppings
        Scanner inText = new Scanner(System.in);                                                            //Creat Scanner for input of Name of Customers

        String CustomersName = " ";
            System.out.print( "Please enter customers name ==> ") ;
            CustomersName = inText.nextLine();                                                              //Inputing Customers Name

        String PizzaSize = " ";
            System.out.printf("\n Enter small, medium or large (\"s\",\"m\",\"l\")" + CustomersName + " ==> ");
            PizzaSize = inText.nextLine();

        System.out.printf("\nPlease enter number of toppings for " + CustomersName + " ==> ");              //out putting customers name and number of toppings
            NumberOfTopings = inNumber.nextInt();

            if (PizzaSize.equalsIgnoreCase("s"))                                                            // if statement to calculate if you order a small medium or large pizza order and the following size od sides on pizza
        {
            Charge = Small + (NumberOfTopings * SmallToping);
        }
            else if (PizzaSize.equalsIgnoreCase("m"))
        {
            Charge = Medium + (NumberOfTopings * MediumToping);
        }
            else if (PizzaSize.equalsIgnoreCase("l"))
        {
            Charge = Large + (NumberOfTopings * LargeToping);
        }

        System.out.println("\n\n---Rocky WoodFired Pizza's Reciept---");                                    //Recipt for pizza order with name number of topings and overal price
        System.out.println("Customer Name: " + CustomersName);
        System.out.println("Pizza Size:" + PizzaSize);
        System.out.println("Number of Toppings: " + NumberOfTopings);
        System.out.printf("Total Charge" + " $%.2f", Charge)    ;
    }
}

java中的
final
关键字在变量上指定时,表示该变量的值不能更改(可以将其视为常量值)

我看到您声明了
final double n=6double
表示可以有十进制值的数字,例如
123.456
int
表示整数。如果您试图在for循环中使用
n
,请将
final double n=6
更改为
final int n=6

然后,要在循环中使用它(例如循环
n次
),可以执行以下操作:

for (int i = 0; i < n; i++) {
    // do something
}
int i = 0;
while (i < n) {
    // do something
    i++;
}
for(int i=0;i
在这种情况下,将在循环体中运行内容6次。如果希望在while循环中执行此操作,则可以执行以下操作:

for (int i = 0; i < n; i++) {
    // do something
}
int i = 0;
while (i < n) {
    // do something
    i++;
}
inti=0;
而(i
这个while循环将执行与上面for循环完全相同的操作


我强烈建议您查找其他资源以帮助您学习。web上有大量java教程和其他有关java的初学者信息,因此在这里发布之前请尝试使用它们。

您已经调用了扫描仪两次,无需执行以下操作:

        Scanner inText = new Scanner(System.in); 

您已经完成了一次,现在您可以将其称为
int
next/nextLine

您的代码中不需要两个
Scanner
实例。不要将
Scanner#nextInt()
Scanner#nextLine()
混合在同一代码中,因为如果不格外小心,可能会产生意外的结果。不清楚你的问题是关于什么的。请更具体一点,所以我只想能够循环这个代码6次,所以我有6次orders@TXITECH变量名应以小写开头,以免与以大写开头的类名混淆。谢谢您的评论,非常有见地。我一直在做研究,但我只是有点卡住了,只是为了确定我应该把while循环放在末尾,还是全部循环,还是放在开头?这取决于你想做什么。如果您试图将某个内容循环
n次
,则上述for循环和上述while循环是相同的。我不太明白你对这个任务的要求是什么,但我的主要观点是,
for
循环在语义上等同于使用计数器的while循环。这可能是个问题