小编Tua*_*yen的帖子

将while循环转换为for循环

有一个简单的while循环并尝试使其成为for循环

i=1
while(i<=128)
{     printf("%d",i);
   i*=2;
}
Run Code Online (Sandbox Code Playgroud)

这是我的for循环

for (i=1;i<=128;i++)
{ 
   printf("%d",i);
   i*=2;
}
Run Code Online (Sandbox Code Playgroud)

怎么不给出相同的输出?第一个会打印1248163264128,for循环打印137153163127

c for-loop while-loop

3
推荐指数
2
解决办法
5458
查看次数

为什么这不是创造一个对象?

有人可以查看我的代码中我试图取消和关闭帐户的部分吗?这就是我想要实现的目标:

"2.向您的Account类添加一个方法void close().此方法应通过将"CLOSED"附加到帐户名并将余额设置为0来关闭当前帐户.(帐号应保持不变.)同时减少帐户总数."

当我尝试编译它时,该程序给了我一个关于标识符的错误.我错过了什么?

//*******************************************************/
// Account.java
//
// A bank account class with methods to deposit to, withdraw from,
// change the name on, and get a String representation
// of the account.
//*******************************************************/

public class Account
{
  private double balance;
  private String name;
  private long acctNum;

  //----------------------------------------------
  //Constructor -- initializes balance, owner, and account number
  //----------------------------------------------
  public Account(double initBal, String owner, long number)
  {
    balance = initBal;
    name = owner;
    acctNum = number;
  }

  //----------------------------------------------
  // Checks to …
Run Code Online (Sandbox Code Playgroud)

java

2
推荐指数
1
解决办法
442
查看次数

程序计算能力为2

我正在编写一个简单的程序来计算2的幂.用户输入他们想要计算2的幂的次数,假设用户输入4,我的程序将返回2,4,8,16.这里是代码

import java.util.Scanner;

public class PowersOf2

{

public static void main(String[] args)

{

int numPowersOf2;        
//How many powers of 2 to compute

int nextPowerOf2 = 1;    
//Current power of  2

int exponent = 0;            

//Exponent for current power of 2 -- this

//also serves as a counter for the loop

Scanner scan = new Scanner(System.in);

        System.out.println("How many powers of 2 would you like printed?");
        numPowersOf2 = scan.nextInt();

        //print a message saying how many powers of 2 will be printed
        //initialize …
Run Code Online (Sandbox Code Playgroud)

java math loops

1
推荐指数
1
解决办法
3万
查看次数

帮助使用Arrays for Java

// ****************************************************************
// Sales.java
//
// Reads in and stores sales for each of 5 salespeople.  Displays
// sales entered by salesperson id and total sales for all salespeople.
//
// ****************************************************************
import java.util.Scanner;

public class Sales
{
    public static void main(String[] args)
    {
    final int SALESPEOPLE = 5;
    int[] sales = new int[SALESPEOPLE];
    int sum;
    int average;
    int max=sales[0];
    int min=sales[0];
    int salemade;
    Scanner scan = new Scanner(System.in);

    for (int i=0; i<sales.length; i++)
        {
        System.out.print("Enter sales for salesperson " …
Run Code Online (Sandbox Code Playgroud)

java

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

无限循环,不会以while循环结束

好的,所以我写了这个程序,它将计算一定的字母和空格,我想要它做的是让用户继续输入短语,它继续循环,直到用户进入退出终止.我很难看到在哪里放置while循环.我知道我应该在while循环下嵌套所有循环,当我这样做时,程序进入无限循环.

import java.util.Scanner;

public class Count
{
  public static void main (String[] args)
  {
      String phrase;    // a string of characters
      int countBlank;   // the number of blanks (spaces) in the phrase 
      int length;       // the length of the phrase
      char ch;         // an individual character in the string
        int countA=0,countE=0,countS=0,countT=0;


    Scanner scan = new Scanner(System.in);
      // Print a program header
      System.out.println ();
      System.out.println ("Character Counter");
      System.out.println ();

      // Read in a string and find its length
      System.out.print ("Enter a …
Run Code Online (Sandbox Code Playgroud)

java

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

Java构造函数帮助

//*******************************************************
// Account.java
//
// A bank account class with methods to deposit to, withdraw from,
// change the name on, and get a String representation
// of the account.
//*******************************************************
import java.util.Random;
public class Account
{
  private double balance;
  private String name;
  private long acctNum;

  //----------------------------------------------
  //Constructor -- initializes balance, owner, and account number
  //----------------------------------------------
  public Account(double initBal, String owner, long number)
  {
    balance = initBal;
    name = owner;
    acctNum = number;
  }

  //----------------------------------------------
  // Checks to see if …
Run Code Online (Sandbox Code Playgroud)

java constructor

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

C中的一个简单程序需要一些输入

我正在尝试编写一个简单的程序,它要求用户输入一组正整数,并计算输入的所有数字的平均值.当用户输入非正数(如0或-1)时,程序将终止.

这是我的代码.出于某种原因,当我尝试输入第一个输入时,我收到错误,有人可以帮忙吗?

#include <stdio.h>

int main()
{
  int input=0, sum=0,average=0,i=0;

  printf("Please enter positive numbers, enter 0 or -1 to end:\n");

  scanf("%d",input);

  while (input>0) 
  {
    sum+=input;
    i++;
    scanf("%d",input);
  }
  average=sum/i;
  printf("The average is %d",average);
}
Run Code Online (Sandbox Code Playgroud)

c

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

标签 统计

java ×5

c ×2

constructor ×1

for-loop ×1

loops ×1

math ×1

while-loop ×1