『壹』 java 小程序誤區。。FileInputStream in = null; try { in = new FileInputStream(。。。。。為什麼這么定

你好,這就是變數作用域的意思。
你的定義,變數f只能在你第一個try{}中是可見的,其餘地方引用就會報錯,而正確的寫法就將變數f定義在了try外面,導致f的作用域變為f所在的那層{},在兩個try中都是可見的。

『貳』 java小程序出錯,加try catch以後就編譯出錯,希望高手指點!到底是哪裡有問題呢

double d2 = Double.parseDouble(args [2]);也要放在try里,也有可能出現轉換異常的。

『叄』 一Java小程序出現異常

你{ }的范圍有點問題

『肆』 JAVA 應用小程序applet 運行時報錯

不是的,你的getText沒有填寫值,是個空串,所以你怎麼能讓空串轉換為數字呢?
這個肯定是有異常的

所以try{

intnumber=Integer.valueOf(tf.getText()).intValue();
}catch(NumberFormatExceptionnfe){
System.out.println(nfe.toString());//捕獲NumberFormatException異常
}


『伍』 小程序用了幾分鍾就自己退了,怎麼回事

控制台程序貌似就是這樣的,你可以用遞歸:
static void Main(string[] args)
{
Ac();
}
public static void Ac()
{
const int i = 18;
const int j = 30;
const int k = 50;
int YouAge = 0;
Console.WriteLine("請輸入你的年齡:");
YouAge = int.Parse(Console.ReadLine());
if (YouAge <= i)
{
Console.WriteLine("您的年齡還小,請繼續努力哦!");
}
else
{
if (i < YouAge && YouAge <= j)
{
Console.WriteLine("您現在的階段正是努力奮斗的黃金階段!");
}
else
{
if (j < YouAge && YouAge <= k)
{
Console.WriteLine("您現在正處於人生的黃金階段!");
}
else
{
Console.WriteLine("最美不過夕陽紅!");
}
}
}
Ac();
Console.Read();
}
不過這代碼如果輸入的不是數字就會出錯,最好try{} catth{}一下,不知道你會不會

『陸』 java 關於Process的小程序出現了一個小BUG,求高手幫忙調試下...

這是你這條命令的問題"cmd /c cmd",因為"cmd /c"命令執行後又執行了"cmd"命令,打開了cmd.exe,而cmd.exe沒接到退出命令(如:exit)是不會退出的,所以in.read()會一直讀cmd.exe裡面的輸出信息,導致了死循環的出現.

『柒』 python初學者的一個小程序編寫問題

defcollatz(number):
ifnumber%2==0:
#偶數
#列印出number//2,並返回該值。
print(number//2)
returnnumber//2
else:
#奇數
#列印並返回3*number+1。
print(number*3+1)
returnnumber*3+1

num_return=0

whilenum_return==0:
str_input=input('input:')
try:
num_input=int(str_input)
num_return=collatz(int(num_input))
ifnum_return==1:
break
else:
num_return=0
exceptValueError:
print('請輸入數值')

『捌』 Java 聊天小程序

客戶端:

package clientsocket;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;

public class Main {
private Socket s;
private DataInputStream dis;
private DataOutputStream dos;
private TextArea ta;
private TextField tf;

public static void main(String[] args) {
Main m=new Main();
m.createUI();
m.connect();
m.createThread();

}
public void connect(){
try{s=new Socket("127.0.0.1",8888);//127.0.0.1是你自己的ip號
dos=new DataOutputStream(s.getOutputStream());
dis=new DataInputStream(s.getInputStream());}
catch(IOException e){ e.printStackTrace();}

}
public void createUI(){
Frame f=new Frame("Client");
ta=new TextArea();
tf=new TextField();
Button send=new Button("發送");
Panel p=new Panel();
p.setLayout(new BorderLayout());
p.add(tf,"Center");
p.add(send,"East");
f.add(ta,"Center");
f.add(p,"South");
MyClientListener listener=new MyClientListener(this);
send.addActionListener(listener);
tf.addActionListener(listener);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
f.setSize(400,400);
f.setLocation(600,0);
f.setVisible(true);

}
public void createThread(){
MyClientReader reader=new MyClientReader(this);
reader.start();

}

public void close(){
try{
dis.close();
dos.close();
s.close();
}
catch(IOException e){ e.printStackTrace();}

}

public DataInputStream getDataInputStream(){ return dis;}
public DataOutputStream getDataOutputStream(){return dos;}
public TextArea getTextArea(){return ta;}
public TextField getTextField(){return tf;}
}

class MyClientListener implements ActionListener{
private Main client;
public MyClientListener(Main client){
this.client=client;
}
public void actionPerformed(ActionEvent e){
TextField tf=client.getTextField();
String info=tf.getText();
client.getTextArea().append("自己說:"+info+"\n");
try{
client.getDataOutputStream().writeUTF(info);
}
catch(IOException e1){e1.printStackTrace();}
if (info.equals("bye")){
client.close();
System.exit(0);
}
tf.setText("");
tf.requestFocus();

}

}

class MyClientReader extends Thread {
private Main client;
public MyClientReader(Main client){
this.client=client;
}
public void run(){
String info;
DataInputStream dis=client.getDataInputStream();
TextArea ta=client.getTextArea();
try{
while (true){
info=dis.readUTF();
ta.append("對方說:"+info+"\n");
if (info.equals("bye")){
client.close();
System.exit(0);
}
}
}
catch(IOException e){
e.printStackTrace();
}
}
}

伺服器端:
package serversocket;

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;

public class Main {
private ServerSocket ss;
private Socket s;
private DataInputStream dis;
private DataOutputStream dos;
private TextArea ta;
private TextField tf;

public static void main(String[] args) {
Main m=new Main();
m.createUI();
m.connect();
m.createThread();

}
public void connect(){
try{ss=new ServerSocket(8888); s=ss.accept();
dos=new DataOutputStream(s.getOutputStream());
dis=new DataInputStream(s.getInputStream());}
catch(IOException e){ e.printStackTrace();}

}
public void createUI(){
Frame f=new Frame("Server");
ta=new TextArea();
tf=new TextField();
Button send=new Button("發送");
Panel p=new Panel();
p.setLayout(new BorderLayout());
p.add(tf,"Center");
p.add(send,"East");
f.add(ta,"Center");
f.add(p,"South");
MyServerListener listener=new MyServerListener(this);
send.addActionListener(listener);
tf.addActionListener(listener);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
f.setSize(400,400);
f.setLocation(600,0);
f.setVisible(true);

}
public void createThread(){
MyServerReader reader=new MyServerReader(this);
reader.start();

}

public void close(){
try{
dis.close();
dos.close();
s.close();
ss.close();
}
catch(IOException e){ e.printStackTrace();}

}

public DataInputStream getDataInputStream(){ return dis;}
public DataOutputStream getDataOutputStream(){return dos;}
public TextArea getTextArea(){return ta;}
public TextField getTextField(){return tf;}
}

class MyServerListener implements ActionListener{
private Main client;
public MyServerListener(Main client){
this.client=client;
}
public void actionPerformed(ActionEvent e){
TextField tf=client.getTextField();
String info=tf.getText();
client.getTextArea().append("自己說:"+info+"\n");
try{
client.getDataOutputStream().writeUTF(info);
}
catch(IOException e1){e1.printStackTrace();}
if (info.equals("bye")){
client.close();
System.exit(0);
}
tf.setText("");
tf.requestFocus();

}

}

class MyServerReader extends Thread {
private Main client;
public MyServerReader(Main client){
this.client=client;
}
public void run(){
String info;
DataInputStream dis=client.getDataInputStream();
TextArea ta=client.getTextArea();
try{
while (true){
info=dis.readUTF();
ta.append("對方說:"+info+"\n");
if (info.equals("bye")){
client.close();
System.exit(0);
}
}
}
catch(IOException e){
e.printStackTrace();
}
}
}

要先運行伺服器端,再運行客戶端!

『玖』 求JAVA高手給編個小程序,拋出異常方面的

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

import java.math.*;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author 遙遠de寒星
*/
public class Triangle {
double[] edge = new double[3];
public Triangle(double [] e) throws NotATriangleException{
edge = e;
if(e[0]+e[1]<=e[2]||e[0]+e[2]<=e[1]||e[1]+e[2]<=e[0]){
throw new NotATriangleException();
}
}
public double getArea(){
double p = (edge[0]+edge[1]+edge[2])/2;
double area = Math.sqrt(p*(p-edge[0])*(p-edge[1])*(p-edge[2]));
return area;
}
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
System.out.println("輸入三角形的三邊");
double [] e = new double[3];
for(int i = 0 ; i < 3; i++){
e[i] = scan.nextDouble();
}
try {
Triangle t = new Triangle(e);
System.out.println("面積是"+t.getArea());
} catch (NotATriangleException ex) {
System.out.println("不是三角形");
}
}

}

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author 遙遠de寒星
*/
public class NotATriangleException extends Exception{
public NotATriangleException(){

}
}