Java 在while循环中使用不同的变量创建类对象
我对java相当陌生,所以我知道我的代码不会很好。然而,我正在尝试使用while循环创建一个新的类对象,该循环将像1、2、3等一样作为变量,然后在从网站中提取后,它将具有游戏名称Java 在while循环中使用不同的变量创建类对象,java,Java,我对java相当陌生,所以我知道我的代码不会很好。然而,我正在尝试使用while循环创建一个新的类对象,该循环将像1、2、3等一样作为变量,然后在从网站中提取后,它将具有游戏名称 import java.io.*; import java.net.MalformedURLException; import java.net.URL; public class GameInformationPuller { public static void PullInformation() {
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
public class GameInformationPuller
{
public static void PullInformation() {
String gameName = "";
String gameRating = "";
String gameGenre = "";
int count = 0;
int i = 0;
StringBuilder sb = new StringBuilder();
GameInformation name;
boolean flag;
try {
URL url = new URL("http://www.mmorpg.com/gamelist.cfm/show/all/sCol/titleUC/sOrder/asc");
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
BufferedWriter writer = new BufferedWriter(new FileWriter("C:\\data4.txt"));
String line;
while ((line = reader.readLine()) != null)
{
if(line.contains("http://www.mmorpg.com/gamelist.cfm/game/") || line.contains("<span class=\"value rating\">") || line.contains("title=\"Not enough votes to tabulate\">") || line.contains("<td class=\"genre\">"))
{
if(!line.contains("Jump to Random Game"))
{
sb.delete(0, sb.length());
//------------------------------------------------
if(line.contains("http://www.mmorpg.com/gamelist.cfm/game/"))
{
flag = false;
count = 0;
i = 0;
while(i < line.length() - 1 && flag == false)
{
if(count >= 2 && flag == false)
{
if(line.charAt(i) == '<')
{
flag = true;
}
else
{
if(!(line.charAt(i) == '.'))
{
sb.append(line.charAt(i));
gameName = new String(sb);
}
}
}
if(line.charAt(i) == '>')
{
count++;
}
i++;
}
}
//------------------------------------------------
name = new GameInformation(gameName);
}
//------------------------------------------------
}
else
{
//Nothing
}
}
}
//Close reader and writer
reader.close();
writer.close();
}
catch (MalformedURLException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.*;
导入java.net.MalformedURLException;
导入java.net.URL;
公共类玩家
{
公共静态信息(){
字符串gameName=“”;
字符串gameRating=“”;
字符串gameGenre=“”;
整数计数=0;
int i=0;
StringBuilder sb=新的StringBuilder();
游戏信息名称;
布尔标志;
试一试{
URL=新URL(“http://www.mmorpg.com/gamelist.cfm/show/all/sCol/titleUC/sOrder/asc");
BufferedReader=新的BufferedReader(新的InputStreamReader(url.openStream());
BufferedWriter writer=新的BufferedWriter(新文件写入程序(“C:\\data4.txt”);
弦线;
而((line=reader.readLine())!=null)
{
如果(行)包含(“http://www.mmorpg.com/gamelist.cfm/game/“”| | line.contains(“”| | line.contains(“标题=\”投票数不足,无法制表\“>”)| | line.contains(“”)
{
如果(!line.contains(“跳到随机游戏”))
{
sb.删除(0,sb.length());
//------------------------------------------------
如果(行)包含(“http://www.mmorpg.com/gamelist.cfm/game/"))
{
flag=false;
计数=0;
i=0;
while(i=2&&flag==false)
{
如果(行字符(i)='')
{
计数++;
}
i++;
}
}
//------------------------------------------------
名称=新游戏信息(游戏名称);
}
//------------------------------------------------
}
其他的
{
//没什么
}
}
}
//亲密的读者和作者
reader.close();
writer.close();
}
捕获(格式错误){
e、 printStackTrace();
}
捕获(IOE异常){
e、 printStackTrace();
}
}
}
我希望在此部分找到的每个游戏名称都有一个新变量:
名称=新游戏信息(游戏名称);
希望这是有意义的
谢谢
Simon您可以利用Java集合,例如
ArrayList
。只需将名称定义为:
ArrayList<GameInformation> name; //or names makes more sense
这并没有回答你的问题,但我建议你看看你是否想从网站上获取信息。只是一个语法技巧:flag==false
非常少见,!flag
写起来更短(而且,我认为,可读性更强):)我以后会记得这些提示:)我想我还是习惯了旧的VB方式lol
name.add(new GameInformation(gameName));