java代码怎么打出棱形,java怎么打出菱形
java 怎样写出一个菱形程序?空心和实心
java打印菱形通过二维数组来打印,实例如下:
创新互联是一家专注于成都网站设计、成都做网站与策划设计,阿拉尔网站建设哪家好?创新互联做网站,专注于网站建设十载,网设计领域的专业建站公司;建站业务涵盖:阿拉尔等地区。阿拉尔做网站价格咨询:18980820575
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();
}
}
结果如下:
怎么用java去打印输出一个菱形
首先,这个涉及到行和列,所以,这个肯定是用两个for的嵌套来打印输出。然后,我们要找出这个的规律,因为“磨刀不误砍柴工”,所以找出规律所在,我们才知道应该怎么去下手。比如:我们应该把这个分成上下两个部分,上面部分是:
*****
第一行:三个部分;2个空格 1个*号 2个空格 (i=1)
***** 第二行:三个部分;1个空格 3个*号 1个空格 (i=2)
***** 第三行:三个部分;0个空格 5个*号 0个空格 ( i=3)
为了更加方便去找规律,我们把这个图形补充完整,黑色部分代表着空格,也就是没有显示的部分,而红色部分就是我们的图形的上半部分。所以我们可以得出的规律是:*号的数量和i的关系为:2*i-1 而,空格因为左右一样,所以空格为:3-i。所以上半部分的代码可以这样写:
for(int i =1 ;i=3;i++){
for(int j=1;j=3-i;j++){
System.out.print(" ");//打印左边的空格
}
for(int j=1;j=2*i-1;j++){
System.out.print("*"); //打印*号
}
for(int j=1;j=3-i;j++){
System.out.print(" ");//打印右边空格(也可以省略)
}
System.out.println();//这是每一行打印完就换行
}
那同样的,我们也要在下半部分找出规律来:
***** 第一行:三个部分;1个空格 3个*号 1个空格
(i=1)
***** 第一行:三个部分;2个空格
1个*号 2个空格 (i=2)
所以我们可以得出的规律是:*号的数量和i的关系为:5-2*i
而,空格因为左右一样,所以空格为:i。所以下半部分的代码可以这样写:
for(int i=1;i=2;i++){
for(int j=1;j=i;j++){
System.out.print(" ");
}
for(int j=1;j=5-2*i;j++){
System.out.print("*");
}
for(int j=1;j=i;j++){
System.out.print(" ");
}
System.out.println();
}
同样的任何只要是这样的菱形,我们都可以通过这个规律去打印,只需要把上面的数字替换掉,替换成需要打印的数字就可以了,然后规律大家也可以自己去找。只要很认真熟练地打印出这个例子,那么for的嵌套使用就可以比较熟练了。
如何用java编写用 "*"输出菱形?
参考下面主要代码:
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用三个语句输出菱形
java实现比较键盘输入的3个数的大小,输出大数的步骤如下:
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打印菱形?
java打印菱形方法:
package circulationDemo;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入要打印的行数:");
int num = input.nextInt();
t1(num);// 正直角
split();
t2(num);// 倒直角
split();
t3(num);// 等腰三角
split();
t4(num);// 等腰倒三角++
split();
t5(num);// 等腰倒三角--
split();
t6(num);// 实心菱形
split();
t7(num);// 空心菱形(1)
split();
t8(num);// 空心菱形(2)
split();
t9(num);// 空心菱形(3)
}
private static void t9(int num) {
for (int k = 1; k = num; k++) {
for (int i = 1; i = (num - 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 = num-1; k = 1; k--) {
for (int i = 1; i = (num - 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();
}
}
private static void t8(int num) {
// 上半部分
for (int i = 1; i = num; i++) {
// 打印上半部分空格
for (int j = 1; j = num - i; j++) {
System.out.print(" ");
}
// 打印原本实心的*部分
for (int j = 1; j = 2 * i - 1; j++) {
// 仅在一行的开头和末尾打印*
if (j == 1 || j == 2 * i - 1) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
// 下半部分
for (int i = 1; i = num - 1; i++) {
for (int j = 1; j = i; j++) {
System.out.print(" ");
}
for (int j = 1; j = 2 * (num - i - 1) + 1; j++) {
if (j == 1 || j == 2 * (num - i - 1) + 1) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
private static void t7(int num) {
// 打印上半部分
for (int i = 1; i = num; i++) {
// 打印空格
for (int j = 1; j = (num - i); j++) {
System.out.print(" ");
}
// 打印*的个数
if (i == 1) {
System.out.print("*");
} else {
System.out.print("*");
// 打印菱形里的空格
for (int j = 1; j = (2 * i - 3); j++) {
System.out.print(" ");
}
System.out.print("*");
}
System.out.println();
}
// 打印下半部分
for (int i = num - 1; i 0; i--) {
// 打印空格
for (int j = 1; j = (num - i); j++) {
System.out.print(" ");
}
// 打印*的个数
if (i == 1) {
System.out.print("*");
} else {
System.out.print("*");
// 打印菱形里的空格
for (int j = 1; j = (2 * i - 3); j++) {
System.out.print(" ");
}
System.out.print("*");
}
System.out.println();
}
}
private static void t6(int num) {
// 上半部分
for (int i = 1; i = num; i++) {
for (int j = 1; j = num - i; j++) {
System.out.print(" ");
}
for (int j = 1; j = 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}
// 下半部分
for (int i = 1; i = num - 1; i++) {
for (int j = 1; j = i; j++) {
System.out.print(" ");
}
for (int j = 1; j = 2 * (num - 1 - i) + 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
private static void t5(int num) {
for (int i = num; i 0; i--) {
for (int j = 1; j = num - i; j++) {
System.out.print(" ");
}
for (int j = 1; j = 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
private static void t4(int num) {
for (int i = 1; i = num; i++) {
for (int j = 1; j = i - 1; j++) {
System.out.print(" ");
}
for (int j = 1; j = 2 * (num - i) + 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
private static void t3(int num) {
for (int i = 1; i = num; i++) {
for (int j = 1; j = num - i; j++) {
System.out.print(" ");
}
for (int j = 1; j = 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
private static void split() {
System.out.println("---------------");
}
private static void t2(int num) {
for (int i = 1; i = num; i++) {
for (int j = 1; j = num - i + 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
private static void t1(int num) {
for (int i = 1; i = num; i++) {
for (int j = 1; j = i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
用java编写菱形
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();
网站名称:java代码怎么打出棱形,java怎么打出菱形
文章出自:http://azwzsj.com/article/dsiophe.html