关于JAVA中的LOOP! 菜鸟求助!

pangxie887 2009-04-10 12:39:51
菜鸟又来求助大虾了!!!
关于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 ***
}
...全文
131 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
黑夜愁客 2009-04-10
  • 打赏
  • 举报
回复
这么多,初学者,先看看书本呢
kingssman 2009-04-10
  • 打赏
  • 举报
回复
实在没时间都给你写了,自己研究吧还是
kingssman 2009-04-10
  • 打赏
  • 举报
回复
都是很基础的东西,还是建议你找本书先学习一下吧

public static int sumOfSquaresFor(int lowest, int highest) {
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;
}
macd2008 2009-04-10
  • 打赏
  • 举报
回复
怎么全是注释,不懂
捏造的信仰 2009-04-10
  • 打赏
  • 举报
回复
楼主我教你怎么贴代码:
1、将代码进行良好的格式化,以方便阅读。
2、在发帖文本框的上方单击“#”按钮,选择 Java
3、将代码粘贴到【code=Java】和【/code】之间。发出来的帖子就会是下面的效果:

public class Hello {

// 程序入口
public static void main(String[] args) {
System.out.println("Hello!");
}
}

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧