so i made a simple prog in visual studio.
When I closed down the project it was 100% working
Now having reopened the project it tells me there are errors and wont run
The error codes are CS0101 and CS0111
The prog looks like this
namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
Start:
Random rd = new Random();
double num01 = rd.Next(10,20);
double num02 = rd.Next(1,10);
double ans;
Console.WriteLine("~Press Any Key to Generate Numbers~");
Console.ReadKey();
Console.WriteLine($"What is {num01} divided by {num02}");
ans = Convert.ToDouble (Console.ReadLine());
if (ans == (num01/num02))
{
Console.WriteLine($"Congratulations you are correct!! {num01/num02} \n ~Press Any Key to try again~");
Console.ReadKey();
goto Start;
}
else
{
Console.WriteLine($"You are incorrect! The correct answer is {num01/num02} ~Press Any Key to try again~");
Console.ReadKey();
goto Start;
}
}
}
}
Project was duplicated >_<
Related
public class TulosteluaLikeABoss {
public static void tulostaTahtia(int maara) {
// part 1
int i = 0;
while (maara >i) {
System.out.print("*");
i++;
}
System.out.println("");
}
public static void tulostaTyhjaa(int maara) {
// part 1.1
int i = 0;
while (maara > i) {
System.out.print(" ");
i++;
}
}
//something is wrong below
public static void tulostaKolmio(int koko) {
// part 2
int j = koko;
int k = 0;
while (koko >= k) {
tulostaTahtia(k);
tulostaTyhjaa(j);
k++;
j = j-1;
}
}
// from here below is irrelevant
public static void jouluKuusi(int korkeus) {
// part 3
}
public static void main(String[] args) {
// Testit eivät katso main-metodia, voit muutella tätä vapaasti.
tulostaKolmio(5);
System.out.println("---");
jouluKuusi(4);
System.out.println("---");
jouluKuusi(10);
}
}
I'm trying to do a Java basics course and the task is to print an inward triangle using stars *
I got my program to print that, but when I try to submit, I get error message saying: When tried to call method tulostaKolmio(1), wrong amount of lines were printed. expected <1> but was <2>. I'm pretty annoyed by this, since I ran the code using tulostaKolmio(1) and the program printed just 1 line that had 1 star like it was supposed to. If the code looks strange it's because this is a 3 part task and I'm only doing the second part.
tulostaKolmio(1) assigns koko the value of 1. The while loop in the method will run (koko + 1) number of times. During the first run in the loop tulostaTahita(0) gets called (since k=0 right now), altough it will not print any starts at this call it will print a new line because you have that outside the while loop in the tulostaTahita method.
During the second run in the loop, k=1 and thus tulostaTahita(1) is called. This will print another line so in the end you are left with 2 lines (where the first one is empty).
To solve this you want to add an if statement to make sure tulostaTahita only prints a new line when maara is greater than 0.
I am very new to java as I have only started yesterday and I am trying to make a little game where a random number is generated and you have to try to guess that number. The problem I am having right now is nothing will come out of the console. I am not sure what is causing this as it might be the code or the interpreter I am using. Here is the code for you guys to check over. Let me know what I did wrong and if you can find a fix, thanks.
import java.util.Scanner;
public class Random
{
int Ran = (int) Math.floor(Math.random() * 9);
Scanner input = new Scanner(System.in);
int Num = input.nextInt();
public static void main(String[] args){}
{System.out.println("Geuss a number and see if it is correct!");
}
{
if (Num == Ran)
{System.out.println("Correct! The number was " + Ran);
}
else{
System.out.println("You are wrong!");
}
}
public void If(boolean b) {}
}
you have empty{}
public static void main(String[] args){
System.out.println("Geuss a number and see if it is correct!");
}
Also do not write IF as a function. If is native expression in native coding.
There is already if clause so try to give unique names.
import java.util.Scanner;
public class Random
{
int Ran = (int) Math.floor(Math.random() * 9);
Scanner input = new Scanner(System.in);
int Num = input.nextInt();
public static void main(String[] args)
{
System.out.println("Geuss a number and see if it is correct!");
if (Num == Ran)
{System.out.println("Correct! The number was " + Ran);
}
else{
System.out.println("You are wrong!");
}
}
}
try this.
import java.util.Scanner;
public class Random
{
int Ran = (int) Math.floor(Math.random() * 9);
Scanner input = new Scanner(System.in);
int Num = input.nextInt();
public static void main(String[] args){
System.out.println("Geuss a number and see if it is correct!");
if (Num == Ran)
{System.out.println("Correct! The number was " + Ran);
}
else{
System.out.println("You are wrong!");
}
}
public void If(boolean b) {}
}
}
In codenameone, when using valueOf(String s) on enum throws IllegalArgument exception: No enum const on iPhone5, iOS9, but works fine on simulator and Android. It worked fine 3-4 weeks ago. Type OK in the text box and press the button, on simulator fine, if you build it and run it for iOS9 - you will get the exception.
Bellow a snapshot code to test:
public class MyApplication {
private Form current;
private Resources theme;
enum popo { OK, ERROR,EXCEPTION};
public void init(Object context) {
theme = UIManager.initFirstTheme("/theme");
// Pro only feature, uncomment if you have a pro subscription
// Log.bindCrashProtection(true);
}
public void start() {
if(current != null){
current.show();
return;
}
Form hi = new Form("Hi World");
hi.setLayout(new BorderLayout());
final TextArea input = new TextArea();
Button testr = new Button("Touch me");
testr.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
popo q = popo.valueOf(input.getText());
Dialog.show("title",
"just found string = "+input.getText()+" \nthat gives enum = "+q.toString()
, "OK", null);
}
});
hi.addComponent(BorderLayout.CENTER, input);
hi.addComponent(BorderLayout.SOUTH,testr);
hi.show();
}
public void stop() {
current = Display.getInstance().getCurrent();
}
public void destroy() {
}
}
Many thanks,
Goran.
Enum's values() and related calls aren't implemented in Codename One. The crux of the issue is that they fail during obfuscation (for Android) and require some reflection code generated by the javac tool.
The workaround is to use something like this:
enum popo {OK("OK"), ERROR("ERROR),EXCEPTION("EXCEPTION");
public popo(String value) {
this.value = value;
}
String value;
};
Then use myPopo.value.
So this might be an exceptionally dumb question, but before you burn this post to smolders, please hear me out XD. Below I've written three basic classes, all of which accomplish the same thing, but through various means:
Class A ( break ) -
import java.util.Scanner;
public class ClassA
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
while(true)
{
System.out.print("Enter a sentence (/q to quit): ");
String sentence = in.nextLine();
if(sentence.equals("/q"))
break;
else
System.out.println("Thank you!");
}
}
}
Class B ( return ) -
import java.util.Scanner;
public class ClassB
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
while(true)
{
System.out.print("Enter a sentence (/q to quit): ");
String sentence = in.nextLine();
if(sentence.equals("/q"))
return;
else
System.out.println("Thank you!");
}
}
}
Class C ( System.exit(0) ) -
import java.util.Scanner;
public class ClassC
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
while(true)
{
System.out.print("Enter a sentence (/q to quit): ");
String sentence = in.nextLine();
if(sentence.equals("/q"))
System.exit(0);
else
System.out.println("Thank you!");
}
}
}
While I prefer Classes A and C, is there anything wrong with B? More specifically, is it bad practice to use a return statement to exit from the main method of a program? If so, why?
Thanks in advance!
Using return is exactly the right thing to do, because it makes your main easier to test -- your test framework doesn't need to intercept exit attempts.
We are using continuous integration as part of our build automation. For every check in, the tfs build server builds the project and deploys to our web servers on success.
When the build fails, it automatically creates a new Bug with the details of the build failure.
Due to CI and the activity on the server, this might result in 10 or 20 failure work items before the build starts succeeding again.
So, I have two options. I'd like to either have the build process see if an open work item already exists for a build failure and just add details to that; OR, I'd like the build server to close all of the build failure items automatically when it starts working again.
Any ideas?
You can create a MSBuild Task to do either of these options. Here is a similar piece of code I use to get you started but since I don't know the details of your work item or process you will have to change it.
This code takes all of the work items associated with a build and updates their status.
If you select your first option you can just change the UpdateWorkItemStatus method and update any existing WIs. For the Second method you will need to do a bit more work as you need to look up the prior build rather than take it as a input.
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Build.Utilities;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.Build.Framework;
using Microsoft.TeamFoundation.Build;
using Microsoft.TeamFoundation.Build.Client;
namespace Nowcom.TeamBuild.Tasks
{
public class UpdateWorkItemState: Task
{
private IBuildDetail _Build;
private void test()
{
TeamFoundationServerUrl = "Teamserver";
BuildUri = "vstfs:///Build/Build/1741";
Execute();
}
public override bool Execute()
{
bool result = true;
try
{
TeamFoundationServer tfs = TeamFoundationServerFactory.GetServer(TeamFoundationServerUrl, new UICredentialsProvider());
tfs.EnsureAuthenticated();
WorkItemStore store = (WorkItemStore)tfs.GetService(typeof(WorkItemStore));
IBuildServer buildServer = (IBuildServer)tfs.GetService(typeof(IBuildServer));
_Build = buildServer.GetAllBuildDetails(new Uri(BuildUri));
//add build step
IBuildStep buildStep = InformationNodeConverters.AddBuildStep(_Build, "UpdateWorkItemStatus", "Updating Work Item Status");
try
{
Log.LogMessageFromText(string.Format("Build Number: {0}", _Build.BuildNumber), MessageImportance.Normal);
List<IWorkItemSummary> assocWorkItems = InformationNodeConverters.GetAssociatedWorkItems(_Build);
// update work item status
UpdateWorkItemStatus(store, assocWorkItems, "Open", "Resolved");
SaveWorkItems(store, assocWorkItems);
}
catch (Exception)
{
UpdateBuildStep(buildStep, false);
throw;
}
UpdateBuildStep(buildStep, result);
}
catch (Exception e)
{
result = false;
BuildErrorEventArgs eventArgs;
eventArgs = new BuildErrorEventArgs("", "", BuildEngine.ProjectFileOfTaskNode, BuildEngine.LineNumberOfTaskNode, BuildEngine.ColumnNumberOfTaskNode, 0, 0, string.Format("UpdateWorkItemState failed: {0}", e.Message), "", "");
BuildEngine.LogErrorEvent(eventArgs);
throw;
}
return result;
}
private static void SaveWorkItems(WorkItemStore store, List<IWorkItemSummary> assocWorkItems)
{
foreach (IWorkItemSummary w in assocWorkItems)
{
WorkItem wi = store.GetWorkItem(w.WorkItemId);
if (wi.IsDirty)
{
wi.Save();
}
}
}
// check in this routine if the workitem is a bug created by your CI process. Check by title or assigned to or description depending on your process.
private void UpdateWorkItemStatus(WorkItemStore store, List<IWorkItemSummary> assocWorkItems, string oldState, string newState)
{
foreach (IWorkItemSummary w in assocWorkItems)
{
Log.LogMessageFromText(string.Format("Updating Workitem Id {0}", w.WorkItemId), MessageImportance.Normal);
WorkItem wi = store.GetWorkItem(w.WorkItemId);
if (wi.Fields.Contains("Microsoft.VSTS.Build.IntegrationBuild") && wi.State != "Resolved")
{
wi.Fields["Microsoft.VSTS.Build.IntegrationBuild"].Value =_Build.BuildNumber;
}
if (wi.State == oldState)
{
wi.State = newState;
foreach (Field field in wi.Fields)
{
if (!field.IsValid)
{
break;
}
}
}
if (wi.IsDirty)
{
wi.Save();
}
}
}
private void UpdateBuildStep(IBuildStep step, bool result)
{
step.Status = result ? BuildStepStatus.Succeeded : BuildStepStatus.Failed;
step.FinishTime = DateTime.Now;
step.Save();
}
[Required]
public string BuildUri { get; set; }
[Required]
public string TeamFoundationServerUrl {get; set;}
}
}
< UpdateWorkItemState
TeamFoundationServerUrl="$(TeamFoundationServerUrl)"
BuildUri="$(BuildUri)"
ContinueOnError="false"/>