admin 管理员组文章数量: 887007
java题 急
首先建议自己做。然后现在应该考完了,我来抛砖引玉。package com.imooc.test3;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Test {
public static void main(String[] args) {
System.out.print("请输入投资数(1-1000):");
String input = null;
try {
input = new BufferedReader(new InputStreamReader(System.in)).readLine();
} catch (IOException e) {
e.printStackTrace();
}
if(isNumeric(input)) {
int num = Integer.parseInt(input);
if(num<= 1000 && num>= 1) {
int num0 = getValue();
if(num> num0) {
System.out.println("亏了"+ (num- num0));
} else if(num
System.out.println("赚了"+ (num0- num));
} else {
System.out.println("白干了");
}
} else {
System.out.println("超出范围");
}
} else {
System.out.println("输入的不(全)是数字");
}
}
public static boolean isNumeric(String str) {
for(int i = 0; i
if(!Character.isDigit(str.charAt(i))) {
return false;
}
}
return true;
}
public static int getValue() {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/derby?useUnicode=true&characterEncoding=utf-8";
String usr = "root";
String pwd = "root";
String sql = "select value from mine;";
Connection conn = null;
Statement st = null;
ResultSet rs = null;
int result = 0;
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url, usr, pwd);
st = conn.createStatement();
rs = st.executeQuery(sql);
if(rs.next()) {
result = rs.getInt("value");
}
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if(!rs.isClosed()) {
rs.close();
}
if(!st.isClosed()) {
st.close();
}
if(!conn.isClosed()) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return result;
}
}
本文标签: java题 急
版权声明:本文标题:java题 急 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/jishu/1732356910h1534688.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论