我的代码有什么帮助吗? [英] Any help with my code?

查看:82
本文介绍了我的代码有什么帮助吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好论坛这是我的第一篇帖子我非常兴奋和紧张但无论如何我正在创建一个练习C#的不同东西的练习代码。我对c ++比较熟悉,所以差别让小事变得令人沮丧但是无论如何我在调用我的info()方法时遇到了麻烦,并且在我的集合中执行while循环并获得年龄和体重我更详细地解释了我在代码中使用反斜杠的问题。我是csharp的初学者。请耐心等待:D。提前谢谢。







hello forum this is my first post am pretty excited and nervous but anyways im creating a practice code with different things to practice C#. i am a little more familiar with c++ so the difference gets frustrating with the little things but anyways im having trouble with calling my "info()" method and the do while loops in my set and get for age and weight i explained in more detail the problems im having in the code with backslash. i am a beginner in csharp .please have patience :D. thank you in advance.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace the_helped
{
    class Animal
    {
        private string name;
        private string race;
        private double weight;
        private double age;

        public double _age
        {
            get
            {
                return age;
            }
            set
            {
                if (value > 0 && value <= 60) age = value;
                else
                {
                    do//this do while is not working like i want it too. when an invalid amount is put in by the user it repeats please enter dog's age but immediatly after it says please enter dog's weight (the same this happens with weight but in vice versa) what im trying to accomplish is getting the question "please enter dog's weight: " to be repeated until the if statement is true. for example this is what my program does:"Please enter dog's age: 500(user input)Invalid Age!Dog's Age: 1 "Please enter dog's age: Please enter dog's weight: " until you enter a valid age number i want my program to keep asking instead of asking for the age again but immediatly asking for the weight.The same problem i have with age, i have with weight.
                    {
                        age = 1;
                        Console.WriteLine("Invalid Age!\nDog's age: " + value);
                        Console.Write("Please enter dog's age: ");

                    } while (value < 0 && value > 25);
                }
            }
        }
        public double _weight
        {

            get
            {
                return weight;
            }
            set
            {
                if (value > 0 && value <= 60) weight = value;
                else
                {
                    do
                    {
                        weight = 1;
                        Console.WriteLine("Invalid Weight!\nDog's weight: " + value);
                        Console.Write("Please enter dog's weight: ");

                    } while (value < 0 && value >= 60);

                }



            }
        }

        public Animal()
        {
            Console.Write("\nThese are the constructor details : ");

        }
        public void getdetails()
        {
            age = 1;
            weight = 1;
            Console.WriteLine("\n\nName: No Name");
            Console.WriteLine("Race: No Race");
            Console.WriteLine("Weight: " + weight);
            Console.WriteLine("Age: " + age);
            Console.WriteLine();
        }
        public void info()//heres another problem im having. i know its not necessary but in my practice code i want to call on a method to show the results but its a little different when using different classes in C# so im getting a little confused. its not like c++ where you call the method in main and then put inside the parameters (n,m) and send them to (x,y) method i know its the same concept but its just written differently. i know that the parenthesis are empty but i tried writing inside a variable for each but it was not working so it took them out        
{
            Console.WriteLine(dog.name);
            Console.WriteLine(dog.race);
            Console.WriteLine(dog.weight);
            Console.WriteLine(dog.age);
            Console.ReadLine();
        }


        public static void Main(String[] args)
        {
            Animal dog = new Animal();

            dog.getdetails();

            Console.Write("Please enter your dog's name: ");
            dog.name = Console.ReadLine();
            Console.Write("Please enter your dog's race: ");
            dog.race = Console.ReadLine();
            Console.Write("Please enter your dog's weight: ");
            dog._weight = double.Parse(Console.ReadLine());
            Console.Write("Please enter your dog's age: ");
            dog._age = double.Parse(Console.ReadLine());


            
        }
    }
}

推荐答案

嗯。这在C ++中也是如此:

Hmm. This would do the same in C++:
do
{
    age = 1;
    Console.WriteLine("Invalid Age!\nDog's age: " + value);
    Console.Write("Please enter dog's age: ");
} while (value < 0 && value > 25);

因为你没有在循环中更改的值...除了什么之外,你怎么期望它做什么你知道吗?



你的第二个问题在C ++中也是一样的

Since you don't change the value of value inside the loop...how do you expect it to do anything except what you see?

Your second problem is again the same in C++

public void info()
{
    Console.WriteLine(dog.name);
    Console.WriteLine(dog.race);
    Console.WriteLine(dog.weight);
    Console.WriteLine(dog.age);
    Console.ReadLine();
}

应该使用实例变量,因为它是非静态类方法:

Should be using the instance variables, since it is a non-static class method:

public void info()
{
    Console.WriteLine(name);
    Console.WriteLine(race);
    Console.WriteLine(weight);
    Console.WriteLine(age);
    Console.ReadLine();
}

然后你可以用你的 main 方法调用它:

You then call it from your main method like this:

Animal dog = new Animal();
dog.age= 2.5;
dog.weight = 23.7;
dog.info();





C#对于命名事物也有不同的约定,这对于它来说是一个好主意你可以关注 - 你可以在这里阅读: http:// www。 scribd.com/doc/56782158/All-In-One-Code-Framework-Coding-Standards [ ^ ] - 但基本上属性shoudl以大写字母开头,方法,属性基本字段应该是属性一个unerscore前缀和内部字段shoudl以小写字母开头。



C# also has different conventions for naming things, which it woudl be a good idea for you to follow - you can read it here: http://www.scribd.com/doc/56782158/All-In-One-Code-Framework-Coding-Standards[^] - but basically properties shoudl start with an Uppercase letter, as should methods, property base fields should be the property with an unerscore prefix, and interal fields shoudl start with a lower case letter.


这篇关于我的代码有什么帮助吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆