十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
java实现比较键盘输入的3个数的大小,输出大数的步骤如下:
创新互联于2013年成立,先为海晏等服务建站,海晏等地企业,进行企业商务咨询服务。为海晏企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
1、打开Eclipse,创建一个Java工程,在此工程中新建一个Java类;
2、在新建的Java类中利用Scanner类获取键盘输入的三个数,并且分别赋值给变量a,b,c;
3、添加一个比较两个数的方法,将随机两个数进行比较,再让返回的最大值与最后一个值进行比较,即可得出最大值。
具体实现代码如下:
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
int a,b,c;
Scanner scan = new Scanner(System.in);
System.out.println("请输入三个数");
//键盘输入三个数
a = scan.nextInt();
b = scan.nextInt();
c = scan.nextInt();
System.out.println("最大值为:" + getMax(c, getMax(a, b)));
}
//比较方法
private static int getMax(int x,int y){
return x y ? x : y;
}
}
java 输出菱形代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package demo;
public class Demo01 {
public static void main(String[] args) {
int i, j, k;
for (i = 1; i = 5; i++) {
for (j = 1; j = 5 - i; j++)
System.out.print(" ");
for (k = 1; k = 2 * i - 1; k++)
System.out.print("*");
System.out.println("");
}
for (i = 1; i = 4; i++) {
for (j = 1; j = i; j++)
System.out.print(" ");
for (k = 1; k = 9 - 2 * i; k++)
System.out.print("*");
System.out.println("");
}
}
}
绘制算法:
1、分为两部分,上半部分和下半部分
2、输出空格部分换个输出*部分
3、最后一个标签需要换行
for(i=1;i=4;i++)
{
这个小的for是打印每一行前边的空格
for(j=1;j=4-i;j++)
System.out.print(" ");
这个是打印星星的
for(j=1;j=2*i-1;j++)
System.out.print("*");
这个是打印完一行的换行
System.out.println("");
}
下边的是一样的 只不过每一行的空格和星星数 和上边是翻过来的
for(i=1;i=3;i++)
{
for(j=1;j=i;j++)
System.out.print(" ");
for(j=1;j=7-2*i;j++)
System.out.print("*");
System.out.println("");
}
java.sql.SQLException: 无效的列索引
无效的列索引
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:111)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:145)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:207)
atoracle.jdbc.driver.OraclePreparedStatement.setStringInternal(OraclePreparedStatement.java:5328)
atoracle.jdbc.driver.OraclePreparedStatement.setString(OraclePreparedStatement.java:5320)
at untitled5.addreader1.validID(addreader1.java:128)
at untitled5.addreader1.jButton1_actionPerformed(addreader1.java:97)
at untitled5.addreader1_jButton1_actionAdapter.actionPerformed(addreader1.java:175)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
代码:String driverName = "oracle.jdbc.OracleDriver";
Driver driver = (Driver) Class.forName(driverName).newInstance();
//连接数据库
Connection con = DriverManager.getConnection(
"jdbc:oracle:thin:@LONG:1521:orcl", "sysman", "tongfang");
PreparedStatement pstmt = con.prepareStatement(
" insert into reader values(?)");
pstmt.setString(1, str1);
pstmt.setString(2,str2);
pstmt.setString(3,str3);
pstmt.setString(4,str4);
ResultSet res = pstmt.executeQuery();
public class PrintDiamond2{
public static void main (String args[]){
int TotalLayer=9;
for(int layer=1; layer=TotalLayer; layer++) {
for(int space=1; space=Math.abs((TotalLayer+1)/2-layer); space++){
System.out.print(" ");
}
for(int b=1; b=TotalLayer-2*Math.abs((TotalLayer+1)/2-layer); b++){
System.out.print("*");
}
System.out.println();
}
}
}
用3个for循环打出菱形,可以改变TotalLayer的值来改变菱形的大小
第一个for循环规定多少行
第二个for循环规定每行有多少个空格
第三个for循环规定每行有多少个星号
然后换行,重复2,3循环直到完成第一个循环
最后菱形完成
参考下面主要代码:
public static void draw()
{
int n=10;
for(int i=1;i=n;i++)//控制行数
{
for(int k=n-1;k=i;k--)//打印空格
{
System.out.print(" ");
}
for(int j=1;j=2*i-1;j++)//打印*
{
System.out.print("*");
}
System.out.println();
}
for(int i=n-1;i=1;i--)
{
for(int k=i;k=n-1;k++)
{
System.out.print(" ");
}
for(int j=1;j=2*i-1;j++)
{
System.out.print("*");
}
System.out.println();
}
}
java打印菱形通过二维数组来打印,实例如下:
int hangshu = 11;
int yiban = hangshu / 2 + 1;
int yibanduo = hangshu / 2;
System.out.println("空心菱形:");
for(int k = 1;k = yiban;k++){
for(int i = 1;i = (yiban - k);i++){
System.out.print(" ");
}
System.out.print("*");
for(int i = 1;i = ( (k - 2) * 2 + 1);i++){
System.out.print(" ");
}
if(k != 1){
System.out.print("*");
}
System.out.println();
}
for(int k = yibanduo;k =1;k--){
for(int i = 1;i = (yiban - k);i++){
System.out.print(" ");
}
System.out.print("*");
for(int i = 1;i = ( (k - 2) * 2 + 1);i++){
System.out.print(" ");
}
if(k != 1){
System.out.print("*");
}
System.out.println();
}
System.out.println("实心菱形:");
for(int i = 1;i = hangshu;i++){
if(i = hangshu/2 + 1){
for(int k = 1;k = hangshu/2 + 1- i;k++){
System.out.print(" ");
}
for(int k = 1;k = i;k++){
System.out.print("* ");
}
System.out.println();
}else{
for(int k =1;k = (i -(hangshu/2 + 1));k++){
System.out.print(" ");
}
for(int k = 1;k = (2 *(hangshu/2 + 1) - i);k++){
System.out.print("* ");
}
System.out.println();
}
}
结果如下: