尝试在for循环和失败中实例化对象 [英] Trying to instantiate objects within for loop and failing

查看:147
本文介绍了尝试在for循环和失败中实例化对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  DigitalMain.java:18:不是语句
DigitalPhoto [] class = new DigitalPhoto [9];

编译。我曾经看过ArrayLists,但是我不太确定他们是否会像我试图实现的那样实例化类。在理想的情况下,我将拥有名为class+ i的对象,并通过其内置的set方法为每个对象创建不同的值。

  //导入类$ b $的类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;
字符串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(请输入高度);
picc [i] .setHeight = Double.parseDouble(heightString);
heightString = JOptionPane.showInputDialog(请输入宽度);
picc [i] .setWidth = Double.parseDouble(widthString);
continueQuestion = JOptionPane.showInputDialog(Height:+ picc [i] .getHeight +\\\
Width:+ picc [i] .getWidth +\\\
Resolution:+ picc [i]。 getResolution +\ n Compression Ratio:+ picc [i] .getCompression +\\\
Required Storage:+ picc [i] .calcStorage()+\\\
扫描照片的价格:+ picc [i ] .getCost()+请输入'Y'再试一次或者'Y'来接受值。
};



{
bitpsString = JOptionPane.showInputDialog(请输入您的网络连接速度,必须是1到99999999之间的整数);
bitps = Double.parseDouble(bitpsString);
} while(bitps> 0& bitps< 99999999);
picc0.setSpeed(bitps); (y = 0; 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,您将付款:+ totesPrice +\ nRequired存储是:+ totesSize +传输所需的时间是:+ wholeDigits.format(minutes) +分钟和+ wholeDigits.format(realsec)+秒。



public static double transferTime(double totalStorage,double netSpeed){
double bits,seconds;
bits =(totalStorage * 8);
秒=(bits / netSpeed);
返回秒;
};


解决方案

数组的名称。单词 class 是Java语言中的关键字,因此不能用于命名变量。您也可以像这样使用 ArrayLists
$ b $ pre $ List $< DigitalPhoto> photoes = new ArrayList< DigitalPhoto>();

{
DigitalPhoto photo = new DigitalPhoto();
heightString = JOptionPane.showInputDialog('请输入高度');
photo .setHeight = double.parseDouble(heightString);
heightString = JOptionPane.showInputDialog('请输入宽度');
photo .setWidth = double.parseDouble(widthtString);
photos.add(photo)
continueQuestion = JOptionPane.showInputDialog('Height:'+ class [i] .getHeight +'\\\
\lWidth:'+ class [i] .getWidth +' \\\
\l分辨率:'+ class [i] .getResolution +'\\\
'\\lCompression Ratio:'+ class [i] .getCompression +'\\\
\lRequired Storage:'+ class [i] .calcStorage()+'\\\
\l扫描照片的价格:'+ class [i] .getCost()+'请输入Y再次尝试,或者选择Y来接受值。 $ b} while {cont.equals(Y)};


Basically I am trying to create a new class as long as the continue variable equals "Y". The problem I am having is

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

upon compile. I have looked at ArrayLists but I am not quite sure if they would instantiate classes the same way as what I am trying to achieve. In an ideal situation I would have objects with the name "class"+i, and different values for every object via their built in set methods.

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

解决方案

The issue is most likely the name of your array. The word class is a keyword in the Java language and hence cannot be used to name variables. You can also use ArrayLists like so:

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

这篇关于尝试在for循环和失败中实例化对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆