小编chr*_*ris的帖子

Java do/while循环抵消前两个问题

我在java编程类中编写程序.该程序是一个简单的do/while循环,它使用扫描仪类和带有布尔值的启动读取来询问一些问题.当循环第一次迭代时,它可以正常工作,但是当它第二次迭代时,它会立即显示第一个问题和第二个问题,并且只允许我回答第二个问题.我希望这很清楚.有人有任何见解吗?码:

      /*
      * Programmed By: Cristopher Ontiveros
      * Date: 9/19/13
 * Description: Compute weekly salary and witholding for employees
 */
package project.one.cristopher.ontiveros;

import java.util.Scanner;
public class ProjectOneCristopherOntiveros {

     static Scanner sc = new Scanner(System.in);
     public static void main(String[] args) {
     String fName, lName;
     double rate=0, wPay=0, withholding=0;
     int option = 0;
     boolean badval = true;
         System.out.println("Welcome to the Pay Calculator");


     do{
        System.out.println("Enter employee first name: ");
        fName = sc.nextLine();
        System.out.println("Enter employee last name: ");
        lName = sc.nextLine();
        System.out.println("Enter employee hourly …
Run Code Online (Sandbox Code Playgroud)

java loops boolean

0
推荐指数
1
解决办法
852
查看次数

Java for循环间隔结果

我正在编写Java类中的程序,我需要打印一个星形金字塔.我的代码是:

    Scanner sc = new Scanner(System.in);
    System.out.print("Enter a number between 1 and 20: ");
    int value = sc.nextInt();
    System.out.println("Pattern B: ");
    for(int x = 1; x <= value; x++){
      for(int y = value; y>=x; y-- ){
           System.out.print("*");
     }
       System.out.print("\n");

      }
Run Code Online (Sandbox Code Playgroud)

我的结果打印出一行5星,然后是4,3,2,1(如果用户输入数字5).我想要的是将星星都推到右边.如:

一行5星,(空格)行4星,(两个空格)行3星,(三个空格)行2星,(四个空格)行一星

我有道理吗?

我应该引入if then语句,检查变量y并相应地增加空格吗?如果我让你困惑,我很抱歉.

java for-loop spacing

0
推荐指数
1
解决办法
5000
查看次数

标签 统计

java ×2

boolean ×1

for-loop ×1

loops ×1

spacing ×1