『壹』 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(){

}
}