【问题标题】:Trying to instantiate objects within for loop and failing尝试在 for 循环中实例化对象并失败
【发布时间】:2012-04-30 05:19:38
【问题描述】:

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

DigitalMain.java:18: not a statement
    DigitalPhoto[] class = new DigitalPhoto[9];

编译时。我看过 ArrayLists,但我不太确定它们是否会以与我试图实现的相同的方式实例化类。在理想情况下,我将拥有名称为“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 object loops for-loop


    【解决方案1】:

    class 是关键字 - 您不能将其用作变量名。

    另外,这里有一个奇怪的结构:

    for(int i=0; cont.equals("Y") ; i++)
    {
        ...
    } while {continue.equalsIgnoreCase(Y)};
    

    没有“for/while”循环之类的东西 - 有一个普通的for 循环、一个while 循环和一个do/while 循环。

    所以你实际上有一个for 循环,后面跟着一个无效的while 循环。它没有条件。

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

    你稍后会做类似的事情,虽然这次是do/while

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

    while 循环的条件放在 括号中,而不是大括号:

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

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

    【讨论】:

    • 哈哈哈哇。谢谢你,除了我在最令人震惊的罪犯中修复的无数其他错误。谢谢!
    【解决方案2】:

    问题很可能是您的阵列名称。 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")};
    

    【讨论】:

      【解决方案3】:

      你正在使用什么样的循环。

      for(...)
      {
      ...
      }while();
      

      没有for-while 循环。

      还有你的for 循环条件永远不会变成假。为您的 for 循环设置适当的条件。
      for-while 循环中也存在语法错误。 IE。最后一句话

      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 ;     
      

      【讨论】:

        【解决方案4】:

        变量命名有一些规则Variables

         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.
        

        List of keywords

        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)};
        

        这里我不知道任何for-while循环..只是检查一些基础知识..
        Loops in java

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-11-30
          • 1970-01-01
          • 2018-11-11
          • 2020-07-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多