当前位置:编程学习 > JAVA >>

新手写代码出错,代码

import javax.swing.JOptionPane;

public class TurtorLoop {
public static void main(String[] args) {
int correctCount=0;
int count=0;
long starttime= System.currentTimeMillis();
String output="";

while(count<10){
int num1=(int)(Math.random()*10);
int num2=(int)(Math.random()*10);

if(num1<num2){
int temp = num1;
num1 = num2;
num2 = temp;
}

String answerString = JOptoinPane.showInputDialog ("What is "+num1+"-"+num2+"?");
int answer= Integer.parseInt(answerString);

String replyString;
if (num1 - nun2 == answer ) {
replyString = "You are correct";
correctCount++;
}
else
replyString="Your answer is wrong.\n"+num1+"-"+num2+"should be "+(num1-num2);
JOptionPane.showMessageDialog(null,replyString);
count++;
output = "\n"+num1+"-"+num2+"="+answerString+((num1-num2==answer)?"correct":"wrong");
}

long endTime =System.currentTimeMillis();
long testTime=System.currentTimeMillis();
  JOptionPane.showMessageDialog(null,"Correct count is"+correctCount+
  "\nTest time is"+testTime/1000+"seconds\n"+output);
  }
}

用Eclipse编译运行后,出现如下错误。请多指教!
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
nun2 cannot be resolved to a variable

at TurtorLoop.main(TurtorLoop.java:25)

我是新手请大家多喷一喷,让我成长的更快些。多谢。 --------------------编程问答--------------------
Java code服务端
import java.net.*;
import java.io.*;
import java.util.*;
class fanserver
{
    static List<fanclient> client = new ArrayList<fanclient>();
    public static void main(String[] args)throws Exception 
    {
        Socket s;
        ServerSocket ss = new ServerSocket(4445);
        while(true){
            s = ss.accept();
            System.out.println("haha...");
            server ser = new server(s);
            new Thread(ser).start();
            client.add(ser);
        }
    /*    BufferedReader br = 
            new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bwout = 
            new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
        BufferedReader brin = 
            new BufferedReader(new InputStreamReader(s.getInputStream()));
        String str = null;
        while((str=brin.readLine())!=null){
        //    System.out.println(str);
            bwout.write(str);
            bwout.flush();
            bwout.newLine();
        }
        */
        
    //    s.shutdownOutput();
        
        
    }
}
class server implements Runnable
{
    private Socket s;
    server(Socket s){
        this.s = s;
    }
    public void run(){
        try{
            
            BufferedWriter bwout = 
                new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
            BufferedReader brin = 
                new BufferedReader(new InputStreamReader(s.getInputStream()));
            
            String str = null;
            while((str=brin.readLine())!=null){
                System.out.println(str);
                bwout.write(str);
                bwout.newLine();
                
                bwout.flush();
            }
        //    s.shutdownOutput();
            
        }
        catch(Exception e){}
    }
}
Java code客户端

import java.net.*;
import java.io.*;
class fanclient 
{    

    public static void main(String[] args)throws Exception
    {
        Socket s = new Socket("192.168.30.129",4445);
        BufferedReader br = 
            new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bwout = 
            new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
        BufferedReader brin = 
            new BufferedReader(new InputStreamReader(s.getInputStream()));
        new Thread(new client(brin)).start();
        String str = null;
        while((str=br.readLine())!=null){
            
            bwout.write(str);
            
            bwout.newLine();
            bwout.flush();
            
        }
        
    //    s.shutdownOutput();
    
    //    String line = brin.readLine();
    //    System.out.println(line);
    
    
    }
}
class client implements Runnable
{
    private BufferedReader brin;
    client(BufferedReader brin)
    {
        this.brin = brin;
    }
    public void run(){
        String line = null;
        try{
            while((line=brin.readLine())!=null){
                System.out.println(line);    
            }
        }catch(Exception e){}
    }
}

我同时启动好几个客户端,想让客户端通过服无端互相接收到信息,怎么处理, 
 
 
--------------------编程问答-------------------- nun2改成num2吧,要细心一点哦 --------------------编程问答-------------------- if (num1 - nun2 == answer ) 变为if (num1 - num2 == answer )  --------------------编程问答-------------------- JOptionPane写错了
nun2 改成 num2
--------------------编程问答--------------------

import javax.swing.JOptionPane;

public class TurtorLoop {
public static void main(String[] args) {
int correctCount=0;
int count=0;
long starttime= System.currentTimeMillis();
String output="";

while(count<10){
int num1=(int)(Math.random()*10);
int num2=(int)(Math.random()*10);

if(num1<num2){
int temp = num1;
num1 = num2;
num2 = temp;
}

String answerString = JOptionPane.showInputDialog ("What is "+num1+"-"+num2+"?");
int answer= Integer.parseInt(answerString);

String replyString;
if (num1 - num2 == answer ) {
replyString = "You are correct";
correctCount++;
}
else
replyString="Your answer is wrong.\n"+num1+"-"+num2+"should be "+(num1-num2);
JOptionPane.showMessageDialog(null,replyString);
count++;
output = "\n"+num1+"-"+num2+"="+answerString+((num1-num2==answer)?"correct":"wrong");
}

long endTime =System.currentTimeMillis();
long testTime=System.currentTimeMillis();
  JOptionPane.showMessageDialog(null,"Correct count is"+correctCount+
"\nTest time is"+testTime/1000+"seconds\n"+output);
}
}


用Eclipse应该自动报错的,然后你看看下划线按F2获得焦点看看咋回事呀~
要仔细 --------------------编程问答-------------------- 上面代码突出显示不出来……

import javax.swing.JOptionPane;

public class TurtorLoop {
    public static void main(String[] args) {
        int correctCount=0;
        int count=0;
        long starttime= System.currentTimeMillis();
        String output="";
        
        while(count<10){
        int num1=(int)(Math.random()*10);
        int num2=(int)(Math.random()*10);
        
        if(num1<num2){
        int temp = num1;
        num1 = num2;
        num2 = temp;
        }

        String answerString = JOptionPane.showInputDialog ("What is "+num1+"-"+num2+"?");//是JOptionPane
        int answer= Integer.parseInt(answerString);
        
        String replyString;
        if (num1 - num2 == answer ) {   //是num2
        replyString = "You are correct";
        correctCount++;
        }
        else
        replyString="Your answer is wrong.\n"+num1+"-"+num2+"should be "+(num1-num2);
        JOptionPane.showMessageDialog(null,replyString);
        count++;
        output = "\n"+num1+"-"+num2+"="+answerString+((num1-num2==answer)?"correct":"wrong");
        }
        
        long endTime =System.currentTimeMillis();
        long testTime=System.currentTimeMillis();
          JOptionPane.showMessageDialog(null,"Correct count is"+correctCount+
        "\nTest time is"+testTime/1000+"seconds\n"+output);
    }
}
--------------------编程问答-------------------- nun2 cannot be resolved to a variable

要学会从报错信息里发现关键、有用的信息啊
上面的报错信息就很明确的指出了:“nun2”不能解析为一个变量 --------------------编程问答--------------------   嗯    细节问题  --------------------编程问答-------------------- 学习了,谢谢楼主 --------------------编程问答-------------------- 问题解决了结贴吧就!有时候需要细心的查一下,尤其是log,从中找出与你代码有关的信息,然后仔细检查! --------------------编程问答-------------------- 是这样,一般遇到问题,要先看异常是怎么给你说的。就像这次,说的很明白的。
引用 2 楼  的回复:
nun2改成num2吧,要细心一点哦
--------------------编程问答-------------------- 新手嘛,加油哦!
写代码自己定个标准,写完后编译不要超过三个错误!自己定个框框
补充:Java ,  Java SE
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,