Java 尝试在for循环中实例化对象,但失败

Java 尝试在for循环中实例化对象,但失败,java,object,loops,for-loop,Java,Object,Loops,For Loop,基本上,我试图创建一个新类,只要continue变量等于“Y”。我遇到的问题是 DigitalMain.java:18: not a statement DigitalPhoto[] class = new DigitalPhoto[9]; 编译后。我已经看过ArrayList,但我不确定它们是否会像我试图实现的那样实例化类。在理想情况下,我会有名为“class”+I的对象,每个对象通过其内置的set方法具有不同的值 // Import classes for class import

基本上,我试图创建一个新类,只要continue变量等于“Y”。我遇到的问题是

DigitalMain.java:18: not a statement
    DigitalPhoto[] class = new DigitalPhoto[9];
编译后。我已经看过ArrayList,但我不确定它们是否会像我试图实现的那样实例化类。在理想情况下,我会有名为“class”+I的对象,每个对象通过其内置的set方法具有不同的值

// Import classes for class
import java.util.Arrays;
import java.util.List;
import javax.swing.*;
import java.awt.event.*;
import java.text.DecimalFormat;

public class DigitalMain
{
  public static void main(String args[])
  {
    String cont = "Y";
    String heightString,widthString,width,bitpsString;
    double bitps,x,y,totesPrice,totesSize,totesSpeed;
    DecimalFormat wholeDigits = new DecimalFormat("0");
    DecimalFormat dec = new DecimalFormat("0.00");

    DigitalPhoto[] picc = new DigitalPhoto[20];
    for(int i=0; cont.equals("Y") ; i++)
    {
    picc[i] = new DigitalPhoto();
    heightString = JOptionPane.showInputDialog("Please enter height");
    picc[i].setHeight = Double.parseDouble(heightString);
    heightString = JOptionPane.showInputDialog("Please enter width");
    picc[i].setWidth = Double.parseDouble(widthString);
    continueQuestion = JOptionPane.showInputDialog("Height: " + picc[i].getHeight + "\n Width: " + picc[i].getWidth + "\n Resolution: " + picc[i].getResolution + "\n Compression Ratio: " + picc[i].getCompression + "\n Required Storage: " + picc[i].calcStorage() + "\n Price of Scanned Photo: " + picc[i].getCost() + "Please enter 'Y' to try again or anything but 'Y' to accept values.");
    };


    do
    {
    bitpsString = JOptionPane.showInputDialog("Please enter your internet connection speed. Must be an integer between 1 and 99999999");
    bitps = Double.parseDouble(bitpsString);
    } while (bitps > 0 && bitps < 99999999);
    picc0.setSpeed(bitps);

    for(y = 0; y<picc.length; y++) {
      totesPrice += picc+y.getCost();
      totesSize += picc+y.calcStorage();
      totesSpeed = picc0.getSpeed();
    }

    double seconds = transferTime(totesSize, totesSpeed);
    double minutes = seconds / 60;
    double realsec = seconds % 60;

    JOptionPane.showMessageDialog(null, "You will be paying: " + totesPrice + "\nRequired Storage is: " + totesSize + "Required time for transfer is: " + wholeDigits.format(minutes) + " minutes, and " + wholeDigits.format(realsec) + " seconds.");

  }

  public static double transferTime(double totalStorage, double netSpeed) {
    double bits, seconds;
    bits = (totalStorage * 8);
    seconds = (bits / netSpeed);
    return seconds;
    };
}
//为类导入类
导入java.util.array;
导入java.util.List;
导入javax.swing.*;
导入java.awt.event.*;
导入java.text.DecimalFormat;
公共类DigitalMain
{
公共静态void main(字符串参数[])
{
字符串cont=“Y”;
字符串高度字符串、宽度字符串、宽度、位字符串;
双比特,x,y,TotesSpice,totesSize,totesSpeed;
DecimalFormat wholeDigits=新的DecimalFormat(“0”);
DecimalFormat dec=新的DecimalFormat(“0.00”);
数码照片[]picc=新数码照片[20];
对于(int i=0;cont.equals(“Y”);i++)
{
picc[i]=新数码照片();
heightString=JOptionPane.showInputDialog(“请输入高度”);
picc[i].setHeight=Double.parseDouble(heightString);
heightString=JOptionPane.showInputDialog(“请输入宽度”);
picc[i].setWidth=Double.parseDouble(widthString);
continueQuestion=JOptionPane.showInputDialog(“高度:+picc[i]。getHeight+”\n宽度:+picc[i]。getWidth+“\n分辨率:+picc[i]。getResolution+”\n压缩比:+picc[i]。getCompression+”\n所需存储:+picc[i]。calcStorage()+”\n扫描照片价格:+picc[i]。getCost()“请输入'Y'以重试,或输入除'Y'以外的任何内容以接受值。”);
};
做
{
BitpString=JOptionPane.showInputDialog(“请输入您的internet连接速度。必须是介于1和9999999之间的整数”);
bitps=Double.parseDouble(bitpString);
}而(bitps>0&&bitps<9999999);
picc0.设定速度(比特秒);

for(y=0;y
是一个关键字-不能将其用作变量名

此外,这里还有一个奇怪的构造:

for(int i=0; cont.equals("Y") ; i++)
{
    ...
} while {continue.equalsIgnoreCase(Y)};
没有“for/while”循环-有正常的
for
循环、
while
循环和
do
/
while
循环

实际上,这里有一个
for
循环,后面跟着一个无效的
while
循环,它没有条件

你需要找出你想要的。(可能是一个包含do/while循环的for循环,尽管我会将内部循环提取到一个单独的方法中。通常,你的代码会从被分解成多个方法中受益匪浅。)

稍后您将执行类似的操作,不过这次使用
do
/
while

do
{
    ...
} while {bitps > 0 && bitps < 99999999};

基本上,您应该仔细阅读循环可用的语法选项。

问题很可能是数组的名称。单词
class
是Java语言中的一个关键字,因此不能用于命名变量。您也可以像这样使用
ArrayLists

List<DigitalPhoto> photoes = new ArrayList<DigitalPhoto>(); 
do
    {
    DigitalPhoto photo = new DigitalPhoto();
    heightString = JOptionPane.showInputDialog('Please enter height');
    photo .setHeight = double.parseDouble(heightString);
    heightString = JOptionPane.showInputDialog('Please enter width');
    photo .setWidth = double.parseDouble(widthtString);
    photos.add(photo)
    continueQuestion = JOptionPane.showInputDialog('Height: ' + class[i].getHeight + '\n\lWidth: ' + class[i].getWidth + '\n\l Resolution: ' + class[i].getResolution + '\n\lCompression Ratio: ' + class[i].getCompression + '\n\lRequired Storage: ' + class[i].calcStorage() + '\n\lPrice of Scanned Photo: ' + class[i].getCost() + 'Please enter "Y" to try again or anything but "Y" to accept values.')
    } while {cont.equals("Y")};
List photoes=new ArrayList();
做
{
DigitalPhoto photo=新的DigitalPhoto();
heightString=JOptionPane.showInputDialog('请输入高度');
photo.setHeight=double.parseDouble(heightString);
heightString=JOptionPane.showInputDialog('请输入宽度');
photo.setWidth=double.parseDouble(widthtString);
照片。添加(照片)
continueQuestion=JOptionPane.showInputDialog('Height:'+class[i].getHeight+'\n\lWidth:'+class[i].getWidth+'\n\l分辨率:'+class[i].getResolution+'\n\l压缩比:'+class[i].getCompression+'\n\l所需存储:'+class[i].calcStorage()+'\n\l扫描照片的价格:'+class[i].getCost()+'请输入“Y”“以重试,或除“Y”以外的任何内容以接受值。”)
}而{cont.equals(“Y”)};

您正在使用哪种循环

for(...)
{
...
}while();
while没有
循环

而且您的
for
循环条件永远不会变为false。请为for循环设置适当的条件。
您的
for while
loop.ie.last语句中也存在语法错误

continueQuestion = JOptionPane.showInputDialog('Height: ' + class[i].getHeight + '\n\lWidth: ' + class[i].getWidth + '\n\l Resolution: ' + class[i].getResolution + '\n\lCompression Ratio: ' + class[i].getCompression + '\n\lRequired Storage: ' + class[i].calcStorage() + '\n\lPrice of Scanned Photo: ' + class[i].getCost() + 'Please enter "Y" to try again or anything but "Y" to accept values.') // you miss the ;     

变量的命名有一些规则

在这里,我暂时不知道任何
循环..只需检查一些基本知识..

哈哈哈哇。除了我犯下的无数其他错误之外,我还感谢你,这些错误似乎已经在最恶劣的罪犯中得到了纠正。谢谢!
continueQuestion = JOptionPane.showInputDialog('Height: ' + class[i].getHeight + '\n\lWidth: ' + class[i].getWidth + '\n\l Resolution: ' + class[i].getResolution + '\n\lCompression Ratio: ' + class[i].getCompression + '\n\lRequired Storage: ' + class[i].calcStorage() + '\n\lPrice of Scanned Photo: ' + class[i].getCost() + 'Please enter "Y" to try again or anything but "Y" to accept values.') // you miss the ;     
 You cannot use any of the following(in list of keywords) as identifiers in your
 programs.The keywords const and goto are reserved, even though they are not 
 currently used. true, false, and null might seem like keywords, but they 
 are actually literals;      you cannot use them as identifiers in your programs.
for(int i=0; cont.equals("Y") ; i++)
 {
   class[i] = new DigitalPhoto();
   heightString = JOptionPane.showInputDialog('Please enter height');
   class[i].setHeight = double.parseDouble(heightString);
   heightString = JOptionPane.showInputDialog('Please enter width');
   class[i].setWidth = double.parseDouble(widthtString);
   continueQuestion = JOptionPane.showInputDialog('Height: ' + 
   class[i].getHeight + '\n\lWidth: ' + class[i].getWidth + '\n\l Resolution: ' +
   class[i].getResolution + '\n\lCompression Ratio: ' +  class[i].getCompression +
  '\n\lRequired Storage: ' + class[i].calcStorage() + '\n\lPrice of Scanned Photo: ' + 
    class[i].getCost() + 'Please enter "Y" to 
   try again or anything but "Y" to accept values.')
} while {continue.equalsIgnoreCase(Y)};