![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Sep 2005
Location: Anchorage, Alaska
Posts: 37
Rep Power: 0
![]() |
Quick java program
This is a program that I wrote for my cs101 class. I had help with it because I've never written in java before.
/*
* Assignment.java
*
* Created on November 10, 2005, 1:00 PM
*
* This program was writen for an assignment for my CS 101 class.
* Assignment asks the user for a whole number and returns the whole
* plus each value between the number given and 0 (for example: the
* user enters in '3', this program takes three and adds ((((3)+ 2)+ 1)+ 0)).
*
*/
/**
*
* author Alvin D. Morris
* Credit also to my friend Kurt who helped me with my java noobie-ness.
*
*/
import java.util.*;
public class Assignment
{
// Method.
public static int yeah(int y)
{
int counter = 0;
// tv = total value.
int tv = 0;
// while loop adds the counter to the total value and then adds one to the
// counter until the counter is >= y.
while(counter <= y)
{
tv = tv + counter;
counter++;
}
return tv;
}
// Main body of Program.
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
int x;
int y = 0;
// do loop does everything in the loop at least once.
do
{
System.out.println("Type in a whole number (negative to quit): ");
x = kb.nextInt();
// if statement.
if(x >= 0)
{
y = yeah(x);
System.out.println(y);
}
// while loop.
}while (x >= 0);
}
}any critiques or comments welcome... I'm mainly looking for critiques ![]() |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
My only critique is I think instead of a while loop inside of the yea(int) function a for loop would have been better...
public static int yea(int y) {
for(int counter=0,tv=0;counter<=y;counter++)
tv += counter;
}
__________________
|
|
|
|
|
|
#3 | |
|
Programmer
Join Date: Aug 2005
Location: 0x0010 * 0x0091 + 0x0004
Posts: 65
Rep Power: 4
![]() |
Quote:
// tv = total value. int tv = 0; Coding is not so much that your code is functional, but is also about the comments being useful, style, and so on. Otherwise, good job.
__________________
#if 0 /* in case someone actually tries to compile this */ <Jim_McNeat> Is there like a way to put a compiler in "Just trust me on that one" mode? |
|
|
|
|
|
|
#4 | |
|
Programmer
Join Date: Sep 2005
Location: Anchorage, Alaska
Posts: 37
Rep Power: 0
![]() |
Quote:
I tried replacing the while loop with the for loop like in your example but I got a bunch of errors and I didn't quite understand the errors because when I went back to fix the errors I couldn't find anything wrong.... but I will keep this in mind for my next program.... Thanks ![]() |
|
|
|
|
|
|
#5 | |
|
Programmer
Join Date: Sep 2005
Location: Anchorage, Alaska
Posts: 37
Rep Power: 0
![]() |
Quote:
good point... thank you. I will change it to total instead of total value ![]() |
|
|
|
|
|
|
#6 | |
|
Programming Guru
![]() |
Quote:
[php] import java.io.*; public class test { public static void main(String args[]) { System.out.println(yeah(3)); } public static int yeah(int y) { int tv=0; for(int counter=0;counter<=y;counter++) tv += counter; return tv; } } [/php]
__________________
|
|
|
|
|
|
|
#7 |
|
Programmer
Join Date: Sep 2005
Location: Anchorage, Alaska
Posts: 37
Rep Power: 0
![]() |
haha.... thx.... I'll keep this in mind.....
I know about the java.util but whats the java.io? |
|
|
|
|
|
#8 |
|
Professional Programmer
|
__________________
Don't take life too seriously, it's not permanent ! |
|
|
|
|
|
#9 |
|
Newbie
Join Date: Oct 2005
Posts: 2
Rep Power: 0
![]() |
Recursion is beautiful.
public static int sum(int n) {
return (n > 0) ? n + sum(n-1) : 0;
}
__________________
Segmentation fault (core dumped) |
|
|
|
|
|
#10 |
|
Professional Programmer
|
seems pointless ...
__________________
Don't take life too seriously, it's not permanent ! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|