菜鸟又来求助大虾了!!!
关于JAVA中的LOOP
初学者目前在学习,刚接触编程
这几题自己也琢磨了一下,
可是不知道对不对!!
希望大家给我个答案
我参考一下!!!
谢谢!
/**
* Returns the sum of the square of numbers between two values (inclusive)
* So for 3 and 5, the result is 50 (3*3 + 4*4 + 5*5)
* Or for 2 and 7, the result is 139 (2*2 + 3*3 + 4*4 + 5*5 + 6*6 + 7*7)
*
* You should use a for loop for this method.
*/
public static int sumOfSquaresFor(int lowest, int highest) {
return 0; // *** REPLACE THIS LINE WITH YOUR OWN CODE ***
}
/**
* Another version of sumOfSquares that uses a while loop instead of a for loop
*/
public static int sumOfSquaresWhile(int lowest, int highest) {
return 0; // *** REPLACE THIS LINE WITH YOUR OWN CODE ***
}
/**
* Returns the number of spaces in a String
* Example:
* Input: "We are the champions!"
* Returns: 3
*
* Input: "Until next time..."
* Returns: 2
*/
// HINT: Don't use indexOf, there is an easier way
public static int countOfSpaces(String s) {
return 0; // *** REPLACE THIS LINE WITH YOUR OWN CODE ***
}
/**
* Returns the reverse of a String
* Example:
* Input: "We are the champions!"
* Returns: "!snoipmahc eht era eW"
*
* Input: "Until next time..."
* Returns: "...emit txen litnU"
*/
public static String reverse(String s) {
return ""; // *** REPLACE THIS LINE WITH YOUR OWN CODE ***
}
/**
* Print a rectangle of stars.
* @param numRows number of rows in the rectangle
* @param numColumns number of stars (asterisks) in each row.
*
* For example, printRectangle(3, 5) should output:
*****
*****
*****
*/
public static void printRectangle(int numRows, int numColumns) {
// *** REPLACE THIS LINE WITH YOUR OWN CODE ***
}
/**
* Print a triangle of stars, 1 star in the first row, 2 in the second, etc.
* @param numRows number of rows in the triangle
*
* Shown below (side by side for compactness) are the outputs from calling
* printTriangle(1), printTriangle(3), and printTriangle(6).
* * *
** **
*** ***
****
*****
******
*/
public static void printTriangle(int size) {
// *** REPLACE THIS LINE WITH YOUR OWN CODE ***
}
关于JAVA中的LOOP
初学者目前在学习,刚接触编程
这几题自己也琢磨了一下,
可是不知道对不对!!
希望大家给我个答案
我参考一下!!!
谢谢!
/**
* Returns the sum of the square of numbers between two values (inclusive)
* So for 3 and 5, the result is 50 (3*3 + 4*4 + 5*5)
* Or for 2 and 7, the result is 139 (2*2 + 3*3 + 4*4 + 5*5 + 6*6 + 7*7)
*
* You should use a for loop for this method.
*/
public static int sumOfSquaresFor(int lowest, int highest) {
return 0; // *** REPLACE THIS LINE WITH YOUR OWN CODE ***
}
/**
* Another version of sumOfSquares that uses a while loop instead of a for loop
*/
public static int sumOfSquaresWhile(int lowest, int highest) {
return 0; // *** REPLACE THIS LINE WITH YOUR OWN CODE ***
}
/**
* Returns the number of spaces in a String
* Example:
* Input: "We are the champions!"
* Returns: 3
*
* Input: "Until next time..."
* Returns: 2
*/
// HINT: Don't use indexOf, there is an easier way
public static int countOfSpaces(String s) {
return 0; // *** REPLACE THIS LINE WITH YOUR OWN CODE ***
}
/**
* Returns the reverse of a String
* Example:
* Input: "We are the champions!"
* Returns: "!snoipmahc eht era eW"
*
* Input: "Until next time..."
* Returns: "...emit txen litnU"
*/
public static String reverse(String s) {
return ""; // *** REPLACE THIS LINE WITH YOUR OWN CODE ***
}
/**
* Print a rectangle of stars.
* @param numRows number of rows in the rectangle
* @param numColumns number of stars (asterisks) in each row.
*
* For example, printRectangle(3, 5) should output:
*****
*****
*****
*/
public static void printRectangle(int numRows, int numColumns) {
// *** REPLACE THIS LINE WITH YOUR OWN CODE ***
}
/**
* Print a triangle of stars, 1 star in the first row, 2 in the second, etc.
* @param numRows number of rows in the triangle
*
* Shown below (side by side for compactness) are the outputs from calling
* printTriangle(1), printTriangle(3), and printTriangle(6).
* * *
** **
*** ***
****
*****
******
*/
public static void printTriangle(int size) {
// *** REPLACE THIS LINE WITH YOUR OWN CODE ***
}
1、将代码进行良好的格式化,以方便阅读。
2、在发帖文本框的上方单击“#”按钮,选择 Java
3、将代码粘贴到【code=Java】和【/code】之间。发出来的帖子就会是下面的效果:public class Hello { // 程序入口
public static void main(String[] args) {
System.out.println("Hello!");
}
}
int sum = 0; for (int i = lowest; i <= highest; i++) {
sum = sum + i * i;
} return sum;
} public static int sumOfSquaresWhile(int lowest, int highest) {
int sum = 0; int i = lowest;
while (i <= highest) {
sum = sum + i * i;
i++;
} return sum;
}