-
February 26th, 2002, 08:58 AM
#1
I need help in Visual Basic!!!!
Well, the sotry is this... i was going through my old exam papers when i found an old VB assigment that i have done before and aced it.. so i said why not do it again and see if how much i can remember and i was also damn bored so atleast i had something to do..
BTW am using visual basic 6.0
unfortunately our of 20 questions/tasks there are two which i cant do since i 4got most of what i learnt in Visual Basic..
so im askin if anyone here can show me the code that i need.. (note: these are very simple questions, which is why am so disappointed)
Question:
1) Write a program (no form) using modules that uses a while loop to get a name from the user untill the "end" is entered. The name and the count will be displayed at each iyeration.
When "end" is entered, the program will display "Program terminated"
AND
2) Write a program that uses a while look to accept integer values from the user untill a negative value is entered. Each value entered will be tested to determine whether it is odd or even. When the look terminates the program will print:
i) the number of odd and even numbers entered..
ii) The total of odd and even numbers entered...
Note: i have tried doing these questions but i am always stuck.. there are some stuff i cant recall..
Please write your codes in txt form. if you are using a "form" dont worry, just show me the codes (from there i can figure out what your form looks like)
Thanks
-
February 26th, 2002, 09:53 AM
#2
Member
Ok s0nic, I was bored, unfortunately I dont remember enough vb to do this either, so Im doing it in java.
1.
import javax.swing.JOptionPane
public class Name
{
public static void main(String args[])
{
String name;
int count = 0;
while(!name.equals(end))
{
name = JOptionPane.showInputDialog("Please enter the name");
count++;
System.out.println(name + " " + count);
}
System.out.println("Program terminated");
}
}
-
February 26th, 2002, 12:15 PM
#3
hmm thanks for your contribution
but i really need it in Visual Basic form.. though your source code does give me an idea..
-
February 26th, 2002, 01:31 PM
#4
Hm. I could try the 2nd one though I can't test it now since I'm at school, but here we go:
Code:
dim odds, evens as integer
dim result as integer
while result => 0
select case result / 2
case result \ 2 'same as Int(result/2)
evens = evens + 1
case else
odds = odds + 1
end select
result = ( InputBox("Give an integer. Quit by entering a negative value.") )
wend
odds = odds - 1 'because at the first time it haven't got a value for result and it thinks it as 0 -> odd
msgbox "Odds: " & odds & vbNewline & "Evens: " & evens & vbNewline & "Total: " & odds + evens
I think it goes like this, but if it fails then I think you can fix it. The idea is that if you enter eg number 3, it first calculates 3/2=1.5 and then int(3/2) which is 2 (or I guess it should be, can also be 1 - I can't remember if VB just trims the decimals out) which is different from 1.5 so the number is obviously odd.
-ZeroOne '
Q: Why do computer scientists confuse Christmas and Halloween?
A: Because Oct 31 = Dec 25
-
February 27th, 2002, 12:06 AM
#5
-
February 27th, 2002, 12:41 AM
#6
Member
answer to the first, answer to the second to follow
Sub nameentry()
Dim name
Dim curcount
curcount = 1
Do While name <> "end"
name = InputBox("Current Name Number " & curcount, "Enter name")
curcount = curcount + 1
Loop
MsgBox "program terminated"
End Sub
-
February 27th, 2002, 12:57 AM
#7
Wow thanks again!
Thanks to your comtribution i came up with this code:
Code:
Option Explicit
Dim name As String
Dim count As Integer
Sub main()
count = 1
Do While name <> "end"
name = InputBox("Current Name Number " & count, "Enter name")
count = count + 1
Loop
MsgBox "Program Terminated"
End Sub
Thanks again!!
now can someone answer the second question??
-
February 27th, 2002, 01:01 AM
#8
And as for Zero One's tip.. it all went well.. though i did make some few changes..
Code:
Option Explicit
Dim odds, evens As Integer
Dim result As Integer
Sub main()
While result >= 0
Select Case result / 2
Case result \ 2 'same as Int(result/2)
evens = evens + 1
Case Else
odds = odds + 1
End Select
result = (InputBox("Give an integer. Quit by entering a negative value."))
Wend
odds = odds - 1 'because at the first time it haven't got a value for result and it thinks it as 0 -> odd
MsgBox "Odds: " & odds & vbNewLine & "Evens: " & evens & vbNewLine & "Total: " & odds + evens
End Sub
can u see the changes??? hhah athank you very much guys
-
February 27th, 2002, 04:38 AM
#9
Member
A more efficient and exact way to determine odd or even in vb is to use
Dim isodd As Boolean
isodd = CLng(some value) And 1
you get a boolean result that you can use to increment your count.
-
February 27th, 2002, 04:45 AM
#10
Yeah, i was thinkin of using boolean too but i just dont know how.. i remember using it for the question but i4got how i used it...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|